Skip to content

Instantly share code, notes, and snippets.

@TessMyers
Created October 31, 2014 06:14
Show Gist options
  • Save TessMyers/31aaf38de1de4b9be40a to your computer and use it in GitHub Desktop.
Save TessMyers/31aaf38de1de4b9be40a to your computer and use it in GitHub Desktop.
Pseudoclassical instantiation
// This function does not return anything, but instead creates a new instance of an object with
// the given attributes when the "new" keyword is used.
var Cat = function(){
this.stomach = [];
};
// adds methods to the prototype for this new cat object
Cat.prototype.meow = function(){
console.log("MEOWPURR");
};
Cat.prototype.eat = function(food){
this.stomach.push(food);
};
// make that kitty
var kitty = new Cat();
kitty.meow(); // "MEOWPURR"
kitty.eat('tiny helpless birds');
console.log(kitty.stomach) // ["tiny helpless birds"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment