From: Ry Nohryb on
On Jul 27, 1:32 am, Stanimir Stamenkov <s7a...(a)netscape.net> wrote:
> 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?

If this code runs at the top level (if it's not wrapped in an eval()
nor inside any other function), I'd write it so:

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

//Use MYNS here freely, it's already a global.

})();

The window.MYNS in the var MYNS declaration is there just to make
JSLint happy. If JSLint doesn't "hurt your feelings", you can write it
without the window. :

var MYNS= MYNS || { };

But it (JSLint) will probably complain about the use of an undeclared
MYNS because it's being used prior to its declaration.
--
Jorge.
From: Ry Nohryb on
On Jul 27, 1:49 am, Ry Nohryb <jo...(a)jorgechamorro.com> wrote:
>
> But it (JSLint) will probably complain about the use of an undeclared
> MYNS because it's being used prior to its declaration.

Well, I mean, at that point it's -probably- undefined, but not
undeclared.
--
Jorge.
From: JR on
On Jul 26, 9:26 am, 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?
>
> --
> Stanimir

Hi,
I think of (function () {})() as a means of avoiding global variables.
But if you want / need a global variable then you should follow
Jorge's advice or do that:

(function() {
var global = this;
global.MYNS = { };
// to be continued.
})();

But it seems easier to write only var MYNS = { } than the module
pattern (function() {})()

--
JR
From: RobG on
On Jul 26, 10: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 = { };

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.


>      }
>      // 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...

Just declare it before the function.


> }());
>
> 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?

If you want MYNS to be a native Object object, then you should test
explicitly for that. If you want to be strict, then the following
should run as global code:

var MYNS;
if (typeof MYNS != 'object') {
MYNS = {};
}

Now you know excatly what MYNS is (and may have stomped on someone
else's MYNS, but that's for the user of your code to fix).

Don't fall for junk like the following (which is in a commercial
product in wide use where I work):

if (dwr == null) var dwr = {};

Which infers that variables can be conditionally decalred (they can't)
and depends on the truthiness of undefined == null. The author
probably should have written:

var dwr;
if (typeof dwr == 'undefined') {
dwr = {};
}


Anyhow, it is probably sufficient to use the following as global code:

var MYNS = MYNS || {};


--
Rob
From: Scott Sauyet on
On Jul 26, 8:26 am, 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've had to do this a number of times recently, and this has served my
purposes well:

var MYNS = MYNS || {};

at the top of each JS file that uses the namespace. And I simply tell
JSLint that this is a global variable.

When I can't put the global namespace in a JSLint configuration file,
I often format it as

/* global MYNS */ var MYNS = MYNS || {};

to keep it to a single line.

That seems fairly simple, and I don't know if there are any underlying
issues that I haven't considered, but it works well for me.

--
Scott