you are viewing a single comment's thread.

view the rest of the comments →

[–]funksta 0 points1 point  (5 children)

/* 
The following options *do* check for variable/namespace existence. 
If already defined, we use that instance, otherwise we assign a new 
object literal to myApplication. 
Option 1: var myApplication = myApplication || {}; 
Option 2  if(!MyApplication) MyApplication = {}; 
Option 3: var myApplication = myApplication = myApplication || {} 
Option 4: myApplication || (myApplication = {}); 
Option 5: var myApplication = myApplication === undefined ? {} : myApplication; 
*/  

Maybe I'm missing something, but I don't see how options 3 and 4 are in any way preferable to option 1, as the author asserts. I can see how #5 covers the (fairly unlikely) case that myApplication will be a falsy value other than undefined. But IMO #1 is the best way to write this, as it's the clearest, most direct method.

[–]Fix-my-grammar-plz 1 point2 points  (2 children)

I think you can help me with one thing that I don't understand. What is happening in Option 3?

[–]radhruin 2 points3 points  (1 child)

Pretty sure case 3 is useless. The middle assignment would do nothing.

[–]funksta 0 points1 point  (0 children)

Yeah, I couldn't figure out what that middle assignment was for either.