Pages

Thursday, September 29, 2016

Deconstructing Jack and Jill in JavaScript

function LivingOrganism() {};
LivingOrganism.prototype = { constructor: LivingOrganism, };
LivingOrganism.prototype.isMotile = function() { return undefined; };
LivingOrganism.prototype.chromosomeCount = function() { return undefined; };

function Animal(name) { this.name = name; };
Animal.prototype = Object.create(LivingOrganism.prototype);
Animal.prototype.constructor = Animal;
Animal.prototype.isMotile = function() { return true; };

function Human(name) { Animal.call(this, name); }
Human.prototype = Object.create(Animal.prototype);
Human.prototype.constructor = Human;
Human.prototype.chromosomeCount = function() { return 23; };

var blob = new LivingOrganism();
var jack = new Animal("Jack");
var jill = new Human("Jill");