Created
April 29, 2014 17:48
-
-
Save rorcraft/11407295 to your computer and use it in GitHub Desktop.
node module structure
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
// 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