var beverage = function () {}; Beverage.prototype.boilWater = function () {console.log ("boil Water");}; Beverage.prototype.brew = function () {throw new Error ("Subclass must override this method");}; Beverage.prototype.pourInCup = function () {throw new Error ("Subclass must override this method");}; Beverage.prototype.addCondiments = function () {throw new Error ("Subclass must override this method");}; Beverage.prototype.customerWantsCondiments = function () {return true;}; Beverage.prototype.init = function () {this.boilwater (); This.brew (); This.pourincup (); if ( this.customerwantscondiments) {this.addcondiments ();}}; var Coffee = function () {}; Coffee.prototype = new beverage ();//inherit parent class BeverageCoffee.prototype.boilWater = function () {console.log ("boil Water");}; Coffee.prototype.brew = function () {Console.log ("Brew Coffee with boiling water"); Coffee.prototype.pourInCup = function () {Console.log ("Pour the coffee into the cup"); Coffee.prototype.addCondiments = function () {Console.log ("Add sugar and milk"); var Tea = function () {}; Tea.prototype = new beverage ();//inherit parent class BeverageTea.prototype.boilWater = function () {console.log ("boil Water");}; Tea.prototype.brew = Function () {console.log ("soak tea with boiling water"); Tea.prototype.pourInCup = function () {Console.log ("Pour the tea into the cup"); Tea.prototype.addCondiments = function () {console.log ("Add lemon"); Tea.prototype.customerWantsCondiments = function () {return window.confirm ("Would you like to add seasoning?"); var coffee = new coffee ();//instantiation of Coffeecoffee.init (); var tea = new Tea ();//Instantiate Teatea.init ();
JS Prototypes and Inheritance