From: Stanimir Stamenkov on
As the number of utility functions I write increases I've started to
use a global variable acting as namespace for my functions. Given
my functions are spread in couple of .js files and the order of
loading is not significant (documents may include random combination
of the files) I've wondered how it is best to initialize the
namespace object.

I generally use the following construct in my .js files:

(function () {
if (typeof MYNS === "undefined") {
MYNS = { };
}
// Add stuff to MYNS...
}());

But then running this through JSLint tells me: "'MYNS' is not
defined". Changing the code to:

(function () {
if (typeof this.MYNS === "undefined") {
this.MYNS = { };
}
// Add stuff to MYNS...
}());

results in no JSLint errors but then it lists 'MYNS' as member and
not a global variable, if it matters at all. Which of the both
forms is recommended? Should I just tell JSLint 'MYNS' is a
predefined global?

--
Stanimir
From: Asen Bozhilov on
Stanimir Stamenkov wrote:

> results in no JSLint errors but then it lists 'MYNS' as member and
> not a global variable, if it matters at all.  Which of the both
> forms is recommended?  Should I just tell JSLint 'MYNS' is a
> predefined global?

Both are not good. First, because use undeclared assignment. In the
second approach the problem is in ES5 environment. The `this'
associated with that execution context in strict mode must be
`undefined'. So you will get TypeError.

I don't see any benefits by your code. The order of loading is always
mandatory and the users of your code must be know that. I have never
liked such a definition of a namespace. If I want namespace I do:

var MYNS = {};

It's enough. Should I care about third party conflicts? I don't think
so. If there are third party conflicts this means wrong usage of my
code and my recommendations. And for environment which I suggest to be
used this code I am sure that `MYNS' does not refer both host or built-
in object.

What is your native language? Is it Bulgarian?
From: Ry Nohryb on
On Jul 26, 2:26 pm, Stanimir Stamenkov <s7a...(a)netscape.net> wrote:
> As the number of utility functions I write increases I've started to
> use a global variable acting as namespace for my functions.  Given
> my functions are spread in couple of .js files and the order of
> loading is not significant (documents may include random combination
> of the files) I've wondered how it is best to initialize the
> namespace object.
>
> I generally use the following construct in my .js files:
>
> (function () {
>      if (typeof MYNS === "undefined") {
>          MYNS = { };
>      }
>      // Add stuff to MYNS...
>
> }());
>
> But then running this through JSLint tells me: "'MYNS' is not
> defined".  Changing the code to:
>
> (function () {
>      if (typeof this.MYNS === "undefined") {
>          this.MYNS = { };
>      }
>      // Add stuff to MYNS...
>
> }());
>
> results in no JSLint errors but then it lists 'MYNS' as member and
> not a global variable, if it matters at all.  Which of the both
> forms is recommended?  Should I just tell JSLint 'MYNS' is a
> predefined global?

Use window.MYNS instead of this.MYNS, and, in order to avoid
overwriting/destroying a possibly pre-existing/previously-defined
MYNS :

window.MYNS= window.MYNS || {};

JSLint then won't complain because global vars are properties of the
global object, and the 'window' and 'self' globals are references to
the Global Object itself:

GlobalObject.window === GlobalObject.self === GlobalObject ===
(function(){return this})()

and thus window.someGlobalVarName is only an explicit way of saying
"hey, this is a global var !".
--
Jorge.
From: Stanimir Stamenkov on
Mon, 26 Jul 2010 12:47:48 -0700 (PDT), /Asen Bozhilov/:

> Both are not good. First, because use undeclared assignment. In the
> second approach the problem is in ES5 environment. The `this'
> associated with that execution context in strict mode must be
> `undefined'. So you will get TypeError.

O.k. I understand. I generally don't have such advanced knowledge of
JavaScript. Do you know which browsers currently implement the ES5
specification and support strict mode?

> I don't see any benefits by your code. The order of loading is always
> mandatory and the users of your code must be know that. I have never
> liked such a definition of a namespace. If I want namespace I do:
>
> var MYNS = {};
> (...)

The problem is I have a bunch of .js files which happen to be written by
multiple people and contain just "flying" global functions, and I want
to convince all the guys writing them we should at least start using a
separate space than the global object. Then we could also start
thinking of giving the code finer modularization.

So until we start making more sense of the code organization I want to
introduce a single object acting as global namespace for our own code.
And yes, the files are not loaded in random order but then different
sets of them is included in different documents, so I want more
foolproof fix for the initial namespace problem hence I need to set it
up independently in every file, but in non-destructive manner.

> What is your native language? Is it Bulgarian?

Yes, I'm native Bulgarian speaking.

--
Stanimir
From: Stanimir Stamenkov on
Mon, 26 Jul 2010 20:18:00 +0000 (UTC), /Ry Nohryb/:

> Use window.MYNS instead of this.MYNS, and, in order to avoid
> overwriting/destroying a possibly pre-existing/previously-defined MYNS :
>
> window.MYNS= window.MYNS || {};

I decided to use this.MYNS instead of window.MYNS as in the given case,
and as far as my knowledge goes, 'this' should refer to the global
object and I don't really care (or want to depend) it happens to be the
'window' one having a self references as 'window' and 'self'
properties. But as Asen Bozhilov pointed in another reply, I now see
'this' is no safer to use. I currently have no knowledge of ES5 and how
browsers scripts should operate in that environment, but I'll probably
stick with your suggestion until I get to understand more. :-)

> JSLint then won't complain because global vars are properties of the
> global object, and the 'window' and 'self' globals are references to
> the Global Object itself:
>
> GlobalObject.window === GlobalObject.self === GlobalObject ===
> (function(){return this})()
>
> and thus window.someGlobalVarName is only an explicit way of saying
> "hey, this is a global var !".

JSLint also complained about 'window' not defined until I tell it
'window' is a predefined symbol explicitly, although I had checked the
"Assume a browser" option, also. Is it o.k. if I do:

(function () {
var MYNS = window.MYNS = window.MYNS || { };
MYNS.fooBar = function () {
// ...
};
}());

so I don't have to qualify every MYNS reference in the closure, next?

--
Stanimir