Skip to content

Instantly share code, notes, and snippets.

@rorcraft
Created April 29, 2014 17:48
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 rorcraft/11407295 to your computer and use it in GitHub Desktop.
Save rorcraft/11407295 to your computer and use it in GitHub Desktop.
node module structure
// 1. bind every function
function foo(locale) {
return locale + '-foo';
}
module.exports = function helper(locale) {
return {
foo: foo.bind(null, locale)
}
}
// require('helper')(locale).foo();
----------------------
// 2. nested function
module.exports = function helper(locale) {
function foo() {
return locale + '-foo';
}
return {
foo: foo
}
}
// require('helper')(locale).foo();
-----------------------
// 3. prototype
function Helper(locale) {
this.locale = locale
}
Helper.prototype.foo = function () {
return this.locale + '-foo';
}
module.exports = function (locale) {
return new Helper(locale);
}
// require('helper')(locale).foo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment