Skip to content

Instantly share code, notes, and snippets.

@wwalser
Last active January 4, 2016 21:39
Show Gist options
  • Save wwalser/8681997 to your computer and use it in GitHub Desktop.
Save wwalser/8681997 to your computer and use it in GitHub Desktop.
Protypical inheritance
//As with all inheritance patterns, this isn't encouraged. While worth knowing and understanding,
//delegation is preferred in nearly all cases.
function Person(name) {
this.name = name;
}
Person.prototype.sayName = function(){
console.log(this.name);
}
function Ninja(name) {
this.name = name;
}
Ninja.prototype = new Person();
Ninja.prototype.throwStar = function(){
console.log(this.name + " threw a ninja star!");
}
var wes = new Person("Wes");
var anna = new Ninja("Anna");
wes.sayName();
//`wes`
anna.sayName();
//`anna`
//wes.throwStar();
//TypeError: Object #<Person> has no method 'throwStar'
anna.throwStar();
//`Anna threw a ninja star!`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment