Prev: FYI: Creating circular references is a perfectly OK thing to do.
Next: Volunteers Requested for open source publishing project
From: David Mark on 26 Jul 2010 21:06 On Jul 26, 4:18 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > 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 || {}; You've finally lost it, haven't you Jorge? var MYNS; if (!MYNS) { MYNS = {}; } Or:- var MYNS; MYNS = 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: Not that it matters a whit, but last I checked, JSLint decried the use of - window - as an "implied global". (!) > > GlobalObject.window === GlobalObject.self === GlobalObject === > (function(){return this})() As you've been told a thousand times, that proves nothing. > > and thus window.someGlobalVarName is only an explicit way of saying > "hey, this is a global var !". Get better Jorge!
From: David Mark on 26 Jul 2010 21:08 On Jul 26, 3:47 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote: > 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 the strict mode problem to be an issue as you would have to explicitly require strict mode in the code.
From: David Mark on 26 Jul 2010 21:15 On Jul 26, 8:06 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > 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. You know, for somebody who decries the "decline" of this newsgroup, you should know that waffling over such a simple question/answer is not helping matters at all. ;)
From: David Mark on 26 Jul 2010 21:20 On Jul 26, 8:57 pm, RobG <rg...(a)iinet.net.au> wrote: > 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 = {}; > } Forgot the check for null:- var MYNS; if (!MYNS || 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 = {}; Dojo-esque. :( You know they un-declared all of their globals about a year ago because somebody suggested it would be "faster". :) > > 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 = {}; > } Or just used boolean type conversion as any "falsey" value is going to be undesirable. var dwr; if (!dwr) { dwr = {}; } > > 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.
From: Scott Sauyet on 26 Jul 2010 21:32
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. -- Scott |