Skip to content

Instantly share code, notes, and snippets.

@cerebrl
Last active August 26, 2015 04:56
Show Gist options
  • Save cerebrl/0e50e0f36ccce063790a to your computer and use it in GitHub Desktop.
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
/** ****************************************
* 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