/*** 抽象coffee父類,其實可以不用的*/ function Coffee () {} Coffee.prototype.cost = function() {throw '實現這個方法'; }; /*** 黑咖啡,其實可以不用繼承的;*/ function BlackCoffee () {} // BlackCoffee.prototype = new Coffee(); // BlackCoffee.prototype.constructor = BlackCoffee; BlackCoffee.prototype.cost = function() {return 1.99; }; /*** 糖,其實可以不用繼承的;*/ function Milk (coff) {this.coffee = coff; } // Milk.prototype = new Coffee(); // Milk.prototype.constructor = Milk; Milk.prototype.cost = function() {return this.coffee.cost() + 0.2; }; /*** 可以開店賣咖啡了;*/ var bc = new BlackCoffee(); var addMilk = new Milk(bc); console.log(addMilk.cost());
?