Skip to content

Instantly share code, notes, and snippets.

@pazustep
Created September 16, 2011 14:35
Show Gist options
  • Save pazustep/1222247 to your computer and use it in GitHub Desktop.
Save pazustep/1222247 to your computer and use it in GitHub Desktop.
var MyNamespace = function() {
private_hello = function() {
alert("hello");
}
sayHello = function() {
// We're in the same lexical scope, no this needed
private_hello();
}
// This is the namespace obj. Include only the public funs
return {hello: sayHello};
}
// Instantiation required
var obj = new MyNamespace();
obj.hello();
MyNamespace = (function() {
// private, not exported
var private_hello = function() {
alert("hello");
}
// functions defined here will be exported
var public = {
hello: function() {
// We're in the same lexical scope, no this needed
private_hello();
}
};
return public;
})();
MyNamespace.hello();
var MyNameSpace = {
function1: function() {
alert("hello");
},
function2: function() {
this.function1();
}
}
MyNamespace.function2();
var NS = {
_state: {initial: 0},
something: function() { this._state.initial++ }
}
NS.something();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment