23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 5 ■ MEMBER INHERITANCE<br />

162<br />

Now let’s add four chopped Heath Bars to VanillaBean.prototype in order to change vanilla to Ben<br />

& Jerry’s Vanilla Heath Bar Crunch and coffee to Ben & Jerry’ Coffee Heath Bar Crunch. However, as<br />

Figure 5–11 displays, by doing so we also wind up with Chocolate Heath Bar Crunch and Mint Chocolate<br />

Chip Heath Bar Crunch, because everything that inherits from VanillaBean gets the heathBars member!<br />

var VanillaBean = function(vanilla, cinnamon) {<br />

this.vanilla = [1, "bean", vanilla ? vanilla : "Madagascar Bourbon"];<br />

cinnamon && (this.cinnamon = [1, "stick", "Saigon"]);<br />

};<br />

VanillaBean.prototype = {<br />

heavyCream: [1, "cup", "Organic Valley"],<br />

halfHalf: [2, "cup", "Organic Valley"],<br />

sugar: [5/8, "cup"],<br />

yolks: [6]<br />

};<br />

var Coffee = function(coffee) {<br />

this.coffee = coffee || [1/4, "cup, coarsely ground", "Starbucks Espresso"];<br />

};<br />

Coffee.prototype = new VanillaBean();<br />

var Chocolate = function(cocoa, bittersweet) {<br />

this.cocoa = cocoa || [3/16, "cup", "Callebaut"];<br />

this.bittersweet = bittersweet || [1 + 1/2, "cup", "Callebaut"];<br />

};<br />

Chocolate.prototype = new VanillaBean();<br />

Chocolate.prototype.yolks = [4];<br />

var MintChocolateChunk = function(mint) {<br />

this.mint = mint || [1, "cup", "fresh mint leaves"];<br />

};<br />

MintChocolateChunk.prototype = new Chocolate();<br />

MintChocolateChunk.prototype.vanilla = [1/3, "bean", "Madagascar Bourbon"];<br />

MintChocolateChunk.prototype.bittersweet[0] = 1;<br />

delete MintChocolateChunk.prototype.cocoa;<br />

var vanilla = new VanillaBean();<br />

var coffee = new Coffee();<br />

var chocolate = new Chocolate();<br />

var mintChocolateChunk = new MintChocolateChunk();<br />

VanillaBean.prototype.heathBars = [4, "Heath Bars, chopped in chunks"];<br />

console.dir(vanilla);<br />

console.dir(coffee);<br />

console.dir(chocolate);<br />

console.dir(mintChocolateChunk);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!