What neat ways do you use for declaring JavaScript namespaces. I've come across this one:
if (Foo == null || typeof(Foo) != "object") { var Foo = new Object();}
Is there a more elegant or succinct way of doing this?
Just a bit of fun...
What neat ways do you use for declaring JavaScript namespaces. I've come across this one:
Is there a more elegant or succinct way of doing this? Just a bit of fun... |
|||||||||||||||||||||
|
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.
I like this:
| |||||||||||||||||||||
|
I use the approach found on the Enterprise jQuery site, here Here is their example showing how to declare private & public properties and functions. Everything is done as a self-executing anonymous function.
So if you want to access one of the public members you would just go skillet.fry() or skillet.ingredients What's really cool is that you can now extend the namespace using the exact same syntax.
| |||||||||||||||||||||
|
Another way to do it, which I consider it to be a little bit less restrictive than the object literal form, is this:
The above is pretty much like the module pattern and whether you like it or not, it allows you to expose all your functions as public, while avoiding the rigid structure of an object literal. | |||||||||||||||||||||
|
yes it is:
then you can have
| |||||||||||||||||
|
This is a follow-up to user106826's link to Namespace.js. It seems the project moved to github. The new link is now: http://github.com/smith/namespacedotjs I have been using this simple js helper for my tiny project and so far it seems to be light yet versatile enough to handle namespacing and loading modules/classes. It would be great if it would allow me to import a package into a namespace of my choice, not just the global namespace... sigh, but that's besides the point. It allows you to declare the namespace then define objects/modules in that namespace:
Another option is to declare the namespace and it's contents at once:
For more usage examples, look at the example.js file in the source: http://github.com/smith/namespacedotjs/blob/master/example/sandbox.js | |||||
|
I use this approach:-
External code can then:-
| |||||||||||||||||||||
|
I normally build it in a closure:
My style over the years has had a subtle change since writing this, I now find myself writing the closure like this:
I find this way the public API and implementation are easier to understand, think of the return statement as being a public interface to the implementation. | ||||
|
Here's how Stoyan Stefanov does it in his JavaScript Patterns book which I to be found very good. (Also shows how he does comments that allows for auto generated API documentation, and how to add a method to a custom object's prototype):
| ||||
|
If anyone find this interesting,
You can optionally declare a | |||||||||
|
I created namespace which is inspired by Erlang's modules. It is a very functional approach, but that is is how I write my js these days. It gives a closure a global namespace and exposes a defined set functions within that closure.
| |||
|
http://github.com/smith/namespacedotjs You gotta check that out!! :D | |||||||||
|
You can declare a simple function to providing namespaces.
| |||
|
After porting several of my libraries to different projects, and having to constantly be changing the top level (statically named) namespace, I've switched to using this small (open source) helper function for defining namespaces.
Description of the benefits are at my blog post. You can grab the source code here. One of the benefits I really like is isolation between modules with respect to load order. You can refer to an external module BEFORE it is loaded. And the object reference you get will be filled in when the code is available. | |||||
|
I've written another namespacing library that works a bit more like packages / units do in other languages. It allows you to create a package of Javascript code and the reference that package from other code: hello.js
example.js
Only the second file needs to be included in the page its dependencies (hello.js in this example) will automatically be loaded and the objects exported from those dependencies will be used to populate the arguments of the callback function. You can find the related project here: Packages JS | |||
|
If using a Makefile you can do this.
I prefer to use a Makefile anyway once I get to about 1000 lines because I can effectively comment out large swaths of code by removing a single line in the makefile. It makes it easy to fiddle with stuff. Also, with this technique the namespace only appears once in the prelude so it's easy to change and you don't have to keep repeating it inside the library code. A shell script for live development in the browser when using a makefile:
Add this as a make task 'go' and you can 'make go' to keep your build updated as you code. | ||||
|
YUI has a YUI.namespace function to do this | |||
|