Last active
January 4, 2016 21:39
-
-
Save wwalser/8681997 to your computer and use it in GitHub Desktop.
Protypical inheritance
This file contains hidden or 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
//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