-
-
Save scribblet/8093588 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Module pattern (compared to Constructor)