Last active
August 29, 2015 14:05
-
-
Save kotaroito/c734eec087fe734b5678 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Crepe = function() { | |
this.cost = function() { return 200; }; | |
this.toppings = function() { return []; }; | |
}; | |
var Topping = function(crepe) {}; | |
Topping.prototype = { | |
cost: function () { | |
return this.crepe.cost() + this.settings.cost; | |
}, | |
toppings: function() { | |
var toppings = this.crepe.toppings(); | |
toppings.push(this.settings.topping); | |
return toppings; | |
} | |
}; | |
var ChocolateTopping = function(crepe) { | |
this.crepe = crepe; | |
this.settings = {cost: 50, topping: 'chocolate'}; | |
}; | |
ChocolateTopping.prototype = Topping.prototype; | |
var BananaTopping = function(crepe) { | |
this.crepe = crepe; | |
this.settings = {cost: 80, topping: 'banana'}; | |
}; | |
BananaTopping.prototype = Topping.prototype; | |
var plain = new Crepe(); | |
var favoriteCrepe = new BananaTopping(new ChocolateTopping(plain)); | |
// plain crepe | |
console.log(plain.cost()); | |
console.log(plain.toppings()); | |
// banana chocolate | |
console.log(favoriteCrepe.cost()); | |
console.log(favoriteCrepe.toppings()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment