From: David Mark on
On Jul 26, 9:32 pm, Scott Sauyet <scott.sau...(a)gmail.com> wrote:
> David Mark wrote:
> > On Jul 26, 8:57 pm, RobG <rg...(a)iinet.net.au> wrote:
> >> Anyhow, it is probably sufficient to use the following as global code:
>
> >>   var MYNS = MYNS || {};
>
> > That will work as well.  I don't particularly care for it, but that's
> > a matter of personal taste.
>
> The one practical advantage to it I know of is that it's much easier
> to introduce it in the sort of environment the OP describes, when
> there is a team that needs to be convinced that namespaces are not
> difficult.  It's easy to point to a one-liner to demonstrate how easy
> it is to start using namespaces.
>

If the team is having trouble wrapping their brains around global
variable declarations (a la Jorge), then they've got bigger problems
to sort out. They should also understand that there is no such thing
as a "namespace" in JS. It's a global variable referencing an
object. Calling it something else implies that it is somehow
different from all other native objects, which it is not. That's
where the confusion starts.



From: Stanimir Stamenkov on
Tue, 27 Jul 2010 00:57:31 +0000 (UTC), /RobG/:

> Don't be coy about creating global variables. If you want to create
> one, declare it right up front. Just before the anonymous function, use:
>
> var MYNS;
>
> Note that if MYNS already exists, the extra variable declaration has
> no impact, so it is absolutely safe. Anyone reading your code will see
> it right at the top and know it's yours.

Ah, thanks. I was not sure whether the duplicate declaration in
different files would be permissible, i.e. it wouldn't destroy the value
of previously declared and initialized variable. I should have just
tried it, but it is good to know it is all right in theory, too.

> Anyhow, it is probably sufficient to use the following as global code:
>
> var MYNS = MYNS || {};

So I'll stick with this at the beginning of the file.

--
Stanimir
From: RobG on
On Jul 27, 4:08 pm, Stanimir Stamenkov <s7a...(a)netscape.net> wrote:
> Tue, 27 Jul 2010 00:57:31 +0000 (UTC), /RobG/:
>
> > Don't be coy about creating global variables. If you want to create
> > one, declare it right up front. Just before the anonymous function, use:
>
> > var MYNS;
>
> > Note that if MYNS already exists, the extra variable declaration has
> > no impact, so it is absolutely safe. Anyone reading your code will see
> > it right at the top and know it's yours.
>
> Ah, thanks.  I was not sure whether the duplicate declaration in
> different files would be permissible, i.e. it wouldn't destroy the value
> of previously declared and initialized variable.  I should have just
> tried it, but it is good to know it is all right in theory, too.

You could always read the specification[1]: ECMA-262 ed 3, §10.1.3
Variable Instantiation, the last part of which says:

"If there is already a property of the variable object with
the name of a declared variable, the value of the property
and its attributes are not changed. ... In particular, if a
declared variable has the same name as a declared function
or formal parameter, the variable declaration does not
disturb the existing property"


> > Anyhow, it is probably sufficient to use the following as global code:
>
> > var MYNS = MYNS || {};
>
> So I'll stick with this at the beginning of the file.

Of *each* file. ;-)


1. It's always good to find the relevant part of the spec to confirm
things. It's not an easy document to read and comprehend (I find it
quite difficult). If you keep using it as a reference, and search or
ask here if there are things you can't find or figure out, then bit by
bit it starts to make sense (mostly).


--
Rob
From: Stanimir Stamenkov on
Tue, 27 Jul 2010 07:09:41 +0000 (UTC), /RobG/:
> On Jul 27, 4:08 pm, Stanimir Stamenkov <s7a...(a)netscape.net> wrote:
>> Tue, 27 Jul 2010 00:57:31 +0000 (UTC), /RobG/:
>>
>>> Anyhow, it is probably sufficient to use the following as global code:
>>>
>>> var MYNS = MYNS || {};
>>
>> So I'll stick with this at the beginning of the file.
>
> Of *each* file. ;-)

Yes, my mistake. I've really meant "at beginning of each file". :-)

> 1. It's always good to find the relevant part of the spec to confirm
> things. It's not an easy document to read and comprehend (I find it
> quite difficult). If you keep using it as a reference, and search or
> ask here if there are things you can't find or figure out, then bit by
> bit it starts to make sense (mostly).

Seems the best way to go. Thank you and the rest who provided
valuable guidance.

--
Stanimir
From: Stefan Weiss on
On 27/07/10 03:06, David Mark wrote:
> var MYNS;
>
> if (!MYNS) {
> MYNS = {};
> }
>
> Or:-
>
> var MYNS;
>
> MYNS = MYNS || {};

What's wrong with

var MYNS = MYNS || {};

?


--
stefan