Skip to content

Instantly share code, notes, and snippets.

@scribblet
Forked from calvinmetcalf/modulePattern.js
Last active January 1, 2016 04:39
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 scribblet/8093588 to your computer and use it in GitHub Desktop.
Save scribblet/8093588 to your computer and use it in GitHub Desktop.
function myThing(){
// data hiding
var collection = [];
var api = {};
api.methodOne = function(params){
collection.push(params.argument);
//...
};
api.methodTwo = function(params){
var something = api.methodOne(stuff);
//...
};
return api;
}
Alternative approach
function MyModule() {
// Items is "hidden" from implementors
var items = [];
// A specific interface to items is provided
// Implementors cannot access items directly
return {
pop: function() {
return items.pop();
},
push: function(item) {
return items.push(item);
}
}
}
@scribblet
Copy link
Author

Module pattern (compared to Constructor)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment