Skip to content

Instantly share code, notes, and snippets.

@nie-xin
Forked from nathansmith/module_pattern_init.js
Created August 22, 2016 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nie-xin/89f0b38415f92354bdb38907d7df4e08 to your computer and use it in GitHub Desktop.
Save nie-xin/89f0b38415f92354bdb38907d7df4e08 to your computer and use it in GitHub Desktop.
Init + Module Pattern JS
// JS Module Pattern:
// http://j.mp/module-pattern
// Redefine: $, window, document, undefined.
var APP = (function($, window, document, undefined) {
// Automatically calls all functions in APP.init
$(document).ready(function() {
APP.go();
});
// For use only inside APP.
var PRIVATE_CONSTANT_1 = 'foo';
var PRIVATE_CONSTANT_2 = 'bar';
// Expose contents of APP.
return {
// APP.go
go: function() {
var i, j = this.init;
for (i in j) {
// Run everything in APP.init
j.hasOwnProperty(i) && j[i]();
}
},
// APP.init
init: {
call_automatically_one: function() {
// Called on page-load.
// Can still be called individually, via:
// APP.init.call_automatically_one();
},
call_automatically_two: function() {
// Called on page-load.
// Can still be called individually, via:
// APP.init.call_automatically_two();
}
},
util: {
call_specifically_one: function() {
// Must be called individually, via:
// APP.util.call_specifically_one();
},
call_specifically_two: function() {
// Must be called individually, via:
// APP.util.call_specifically_two();
}
}
};
// Parameters: Zepto/jQuery, window, document.
})(typeof Zepto === 'function' ? Zepto : jQuery, this, this.document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment