Created
September 16, 2011 14:35
-
-
Save pazustep/1222247 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MyNameSpace = { | |
function1: function() { | |
alert("hello"); | |
}, | |
function2: function() { | |
this.function1(); | |
} | |
} | |
MyNamespace.function2(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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