Last active
August 26, 2015 04:56
-
-
Save cerebrl/0e50e0f36ccce063790a to your computer and use it in GitHub Desktop.
An alternative to the Constructor/Prototype pattern typically seen in web applications: a basic module pattern returning an API. JSBin here: http://jsbin.com/nuqizaziko/edit?js,console
This file contains 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
/** **************************************** | |
* Module for say hi feature | |
* @module sayHiFactory | |
* @returns {object} | |
*/ | |
function sayHiFactory() { | |
var greeting = "Basic module says, 'hallo'!" | |
function prvtWriteToConsole() { | |
console.log(greeting); | |
} | |
function pblcSayHi() { | |
privateWriteToConsole(); | |
} | |
return { | |
sayHi: pblcSayHi | |
}; | |
} | |
// module.exports = sayHiFactory(); | |
/** | |
* Pros: no implicit environment, easier mental model, more flexibility left to implementation, no `new` needed | |
* Cons: it's different? It's not what everyone else does? | |
* | |
* Is it programming to an interface? Yup! | |
* Is it composable? Yup! | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment