Skip to content

Instantly share code, notes, and snippets.

@calvimor
Last active September 20, 2016 12:47
Show Gist options
  • Save calvimor/819059b39653202f3882b2e4dee58f12 to your computer and use it in GitHub Desktop.
Save calvimor/819059b39653202f3882b2e4dee58f12 to your computer and use it in GitHub Desktop.
Requirements for prototype chain in JS
'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