small bug

Recommend this page to a friend!

      JS Classes blog  >  The JSClasses site is...  >  All threads  >  small bug  >  (Un) Subscribe thread alerts  
Subject:small bug
Summary:var MyUniqueNamespace.utilities = { } //well... almost
Messages:4
Author:zwfx
Date:2010-08-26 12:36:21
Update:2010-09-07 22:54:26
 

  1. small bug   Reply   Report abuse  
Picture of zwfx zwfx - 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

  2. Re: small bug   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-08-26 20:01:29 - In reply to message 1 from zwfx
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.

  3. Re: small bug   Reply   Report abuse  
Picture of Robert Spoons 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.


  4. Re: small bug   Reply   Report abuse  
Picture of Manuel Lemos 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.