Last active
September 20, 2016 12:47
-
-
Save calvimor/819059b39653202f3882b2e4dee58f12 to your computer and use it in GitHub Desktop.
Requirements for prototype chain in JS
This file contains 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
'use strict'; | |
function Animal (voice) { | |
this.voice = voice || 'Grunt' | |
} | |
Animal.prototype.speak = function () { | |
display(this.voice) | |
} | |
function Cat (name, color) { | |
Animal.call(this, 'Meow') //1 | |
this.name = name; | |
this.color = color; | |
} | |
Cat.prototype = Object.create(Animal.prototype); //2 | |
Cat.prototype.constructor = Cat; // 3 | |
//When we creating the prototype chain, we need lines 1, 2 and 3 | |
var fluffy = new Cat('Fluffy', 'Black'); | |
var muffin = new Cat('Muffin', 'White'); | |
//fluffy.speak(); | |
display(fluffy.__proto__.__proto__) | |
display(Animal.prototype) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment