Skip to content

Instantly share code, notes, and snippets.

@julienetie
Created February 27, 2015 17:11
Show Gist options
  • Save julienetie/85f9c826d8c3c6229606 to your computer and use it in GitHub Desktop.
Save julienetie/85f9c826d8c3c6229606 to your computer and use it in GitHub Desktop.
Prototypal Inheritance
//From http://youtu.be/PMfcsYzj-9M?t=14m48s
var AnswerPrototype = {
constructor: function fn0(value) {
this._val = value;
},
get: function fn1() {
return this._val;
}
};
var lifeAnswers = Object.create(AnswerPrototype);
lifeAnswers.constructor(42);
console.log(lifeAnswers.get()); // 42
var dessertAnswer = Object.create(AnswerPrototype);
dessertAnswer.constructor(3.1459);
console.log(dessertAnswer.get()); // 3.1459
var firmAnswerPrototype = Object.create(AnswerPrototype);
firmAnswerPrototype.get = function fn2() {
return AnswerPrototype.get.call(this) + "!!";
};
var luckyAnswer = Object.create(firmAnswerPrototype);
luckyAnswer.constructor(7);
console.log(luckyAnswer.get()); // 7!!
var magicAnswer = Object.create(firmAnswerPrototype);
magicAnswer.constructor(3);
console.log(magicAnswer.get()); // 3!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment