Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

JavaScript Module Pattern

View 1.scope.js
1 2 3 4 5 6 7
var message = 'Hello!';
 
(function() {
var message = 'Hi!';
 
alert(message);
})();
View 1.scope.js
1 2 3 4 5 6 7
var message = 'Hello!';
 
(function() {
alert(message);
 
var message = 'Hi!';
})();
View 1.scope.js
1 2 3 4 5 6 7 8 9
var message = 'Hello!';
 
(function () {
var message;
 
alert(message);
 
message = 'Hi!';
})();
View 1.scope.js
1 2 3 4 5 6 7 8 9 10 11 12 13
var createPerson = function(name) {
var _name = name;
 
return {
getName: function() {
return _name;
}
};
};
 
var joe = createPerson('Joe');
 
alert(joe.getName());
View 1.scope.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(function(module, undefined) {
// Anything declared at this level is only accessible within this
// function. Efectively, they will be the module's private variables.
 
var _answer = 42;
 
// The module's public interface is then defined by declaring variables on
// the module variable that was passed in.
 
module.getAnswer = function(question) {
return _answer;
};
})(window.module = window.module || {});
 
module.getAnswer('Huh?');
View 1.scope.js
1 2 3 4 5 6 7
(function(m) {
m.doStuff = function() {
// TODO: Stuff
};
})(window.deeply.nested.module);
 
window.deeply.nested.module.doStuff();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.