Skip to content

Instantly share code, notes, and snippets.

@cerebrl
Last active August 26, 2015 04:38
Show Gist options
  • Save cerebrl/94018245c72dd50495d1 to your computer and use it in GitHub Desktop.
Save cerebrl/94018245c72dd50495d1 to your computer and use it in GitHub Desktop.
Making a "class" in JavaScript. What we typically see when we code up the M in MVC.
/** ****************************************
* Constructor for say hi feature.
* @constructor SayHiConstructor
* @returns {object}
*/
function SayHiConstructor() {
this.greeting = "Basic module says, 'hallo'!"
}
SayHi.prototype.sayHi = function sayHi() {
this.writeToConsole());
}
SayHi.prototype.writeToConsole = function writeToConsole() {
console.log(this.greeting);
}
// module.exports = SayHiConstructor;
/**
* Pros: memory effecient for programs
* Cons: dependency on implicit environment, no privates, more complex mental model,
* mutable properties
*
* Gang of Four says ...
* 1) "Program to an 'interface', not an 'implementation'."
* 2) "Favor 'object composition' over 'class inheritance'."
*
* This method does not normally adhere to these principles
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment