Skip to main content
Stack Overflow
  1. About
  2. For Teams

Timeline for How do I declare a namespace in JavaScript?

Current License: CC BY-SA 2.5

14 events
when toggle format what by license comment
Jul 20, 2012 at 5:55 comment added Flash Also worth mentioning that in the above examples this refers to what you would expect in publicFunction, but refers to the global object in internalFunction. I usually add var self = this at the top to avoid confusion.
Sep 12, 2011 at 21:41 comment added BumbleB2na kind of a gotchya with this approach: if you want to access 'public' members or methods within an 'internal' method, you have to add on the 'class' name. Example: ns.publicFunction(); or ns.publicMember;
Aug 12, 2011 at 7:19 comment added Ionuț G. Stan @John Kraft, it's necause of the new keyword in front of the function keyword. Basically, what is doing is that it's declaring an anonymous function (and as a function, it is as well a constructor), and it then immediately invokes it as a constructor using new. As such, the final value that gets stored inside ns is an (unique) instance of that anonymous constructor. Hope it makes sense.
Aug 11, 2011 at 21:46 comment added John Kraft JS Newbie here... why is it that I don't have to type ns().publicFunction(), that is... ns.publicFunction() works.
Apr 15, 2011 at 12:50 comment added Adi Roiban Now if JSLint would not complain about this legit structure everything will be so much better :)
Mar 23, 2011 at 10:20 comment added Ionuț G. Stan This pattern works for singleton objects, but jQuery is a constructor function. In my example you can't call ns(), and jQuery needs this.
Mar 23, 2011 at 8:05 comment added Omu anybody knows why jQuery is not using this approach but the comma separated ?
Nov 7, 2010 at 1:16 comment added Lucent I like this better than the comma separated object container that's voted higher. I don't see any shortcomings in comparison, either. Am I missing something?
Oct 14, 2010 at 14:30 comment added Lawrence Barsanti I like this approach because it allows for private functions, variables, and pseudo-constants (i.e. var API_KEY = 12345;).
Oct 7, 2010 at 3:41 comment added Titi Wangsa bin Damhore this style is compatible with my javascript editor (eclipse), as in, it can do auto formatting and indentation.
Jun 14, 2010 at 8:23 history edited Ionuț G. Stan CC BY-SA 2.5
edited body
May 19, 2009 at 9:21 comment added Ionuț G. Stan 1. There's a difference between OLN and the module pattern. 2. I don't /always/ like OLN as you have to remember to not put the last trailing comma and all your attributes must be initialized with a value (like null or undefined). Also, if you need closures for member functions, you will need small function factories for each of those methods. Another thing is that you must enclose all your control structures inside functions, whereas the above form does not impose that. That's not to say that I don't use OLN, is just that sometimes I don't like it.
May 19, 2009 at 8:50 comment added annakata It's kind of blindsided me that anyone wouldn't love OLN. I just... what's not to love? What's so rigid?
May 19, 2009 at 8:39 history answered Ionuț G. Stan CC BY-SA 2.5

AltStyle によって変換されたページ (->オリジナル) /