|
|
 ZaraWebFX | 2010-08-26 19:59:32 |
...
If you want to go further an even use a sort of namespace emulation in JavaScript, your "class" code could look like this:
if(MyUniqueNamespace === undefined)
var MyUniqueNamespace = { }
if(MyUniqueNamespace.utilities === undefined)
var MyUniqueNamespace.utilities = { }
...
_____________________
it should be
if(MyUniqueNamespace.utilities === undefined)
MyUniqueNamespace.utilities = { }
{without the "var" in front}
kr, zara |
 Manuel Lemos | 2010-08-26 20:01:29 - In reply to message 1 from ZaraWebFX |
| Right, if you included the load the script in the "global" scope, it will not make a difference, but I will fix the example in the article. Thanks for the notice. |
 Robert Spoons | 2010-08-27 07:54:07 - In reply to message 2 from Manuel Lemos |
Two points,
1) if(MyUniqueNamespace === undefined) can result in an
error if MyUniqueNamespace is never declared, as when
the varibale MyUniqueNamespace does not appear somewhere
before the conditional. The error gets reported as
"MyUniqueNamespace is not defined" in FF Crome etc
and as "MyUniqueNamespace is undefined" in IE.
2) if undefined gets redefined:
var undefined = "causes MyUniqueNamespace to remain undefined"
the condtional:
if(MyUniqueNamespace === undefined)
will not give the desired reults as undefined is now redfined.
The first error (1) can be avoided by using
either form of typeof in the condtional:
if(typeof MyUniqueNamespace === "undefined")
or
if(typeof(MyUniqueNamespace) === "undefined")
Either form of use avoids missing declaration
errors and works irregardless of a redefinition
of the undefined property.
|
 Manuel Lemos | 2010-09-07 22:54:26 - In reply to message 3 from Robert Spoons |
Sorry for the delay.
Good point. It has not crossed my mind that undefined could be redefined. I assumed it was a reserved word.
I updated the article as you suggested. Thank you for the feedback. |
|