Problem
How do I make a JavaScript namespace so that my objects and functions aren’t replaced by other objects and functions with the same name? The following are some examples of what I’ve done:
if (Foo == null || typeof(Foo) != "object") { var Foo = new Object();}
Is there a more elegant or concise method to accomplish this?
Asked by Scott McKenzie
Solution #1
I follow the steps outlined on the Enterprise jQuery website:
Here’s an example of how to declare private and public properties and functions from their example. Everything is done as an anonymous function that runs on its own.
(function( skillet, $, undefined ) {
//Private Property
var isHot = true;
//Public Property
skillet.ingredient = "Bacon Strips";
//Public Method
skillet.fry = function() {
var oliveOil;
addItem( "\t\n Butter \n\t" );
addItem( oliveOil );
console.log( "Frying " + skillet.ingredient );
};
//Private Method
function addItem( item ) {
if ( item !== undefined ) {
console.log( "Adding " + $.trim(item) );
}
}
}( window.skillet = window.skillet || {}, jQuery ));
So, if you want to get a hold of one of the public members, just use skillet.fry() or skillet.ingredients.
What’s even better is that you can now use the same technique to extend the namespace.
//Adding new Functionality to the skillet
(function( skillet, $, undefined ) {
//Private Property
var amountOfGrease = "1 Cup";
//Public Method
skillet.toString = function() {
console.log( skillet.quantity + " " +
skillet.ingredient + " & " +
amountOfGrease + " of Grease" );
console.log( isHot ? "Hot" : "Cold" );
};
}( window.skillet = window.skillet || {}, jQuery ));
Answered by Jaco Pretorius
Solution #2
I like this:
var yourNamespace = {
foo: function() {
},
bar: function() {
}
};
...
yourNamespace.foo();
Answered by dfa
Solution #3
Another method, which I believe is a little less restricted than the object literal form, is as follows:
var ns = new function() {
var internalFunction = function() {
};
this.publicFunction = function() {
};
};
The above is similar to the module design, and whether you like it or not, it allows you to make all of your functions public while avoiding the rigidity of an object literal.
Answered by IonuČ› G. Stan
Solution #4
Yes. For example:
var your_namespace = your_namespace || {};
then you’re free to have
var your_namespace = your_namespace || {};
your_namespace.Foo = {toAlert:'test'};
your_namespace.Bar = function(arg)
{
alert(arg);
};
with(your_namespace)
{
Bar(Foo.toAlert);
}
Answered by Alex Pacurar
Solution #5
I usually incorporate it into a closure:
var MYNS = MYNS || {};
MYNS.subns = (function() {
function privateMethod() {
// Do private stuff, or build internal.
return "Message";
}
return {
someProperty: 'prop value',
publicMethod: function() {
return privateMethod() + " stuff";
}
};
})();
Since writing this, my writing style has evolved slightly, and I now find myself penning the closing like this:
var MYNS = MYNS || {};
MYNS.subns = (function() {
var internalState = "Message";
var privateMethod = function() {
// Do private stuff, or build internal.
return internalState;
};
var publicMethod = function() {
return privateMethod() + " stuff";
};
return {
someProperty: 'prop value',
publicMethod: publicMethod
};
})();
This makes the public API and implementation more understandable to me. Consider the return statement to be the implementation’s public interface.
Answered by Brett Ryan
Post is based on https://stackoverflow.com/questions/881515/how-do-i-declare-a-namespace-in-javascript