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 />

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

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

};<br />

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

Now let’s add a vanilla member to override Chocolate.prototype.vanilla. Insofar as we’re<br />

chipping the Callebaut bittersweet chocolate, we need only 1 cup, rather than 1 1/2 cups. So, let’s modify<br />

the first element in the MintChocolateChunk.prototype.bittersweet array. Finally, we don’t want the<br />

cocoa, so pass that member to the delete operator, which we covered in Chapter 3.<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 />

Having done so, let’s create a MintChocolateChunk() instance named mintChocolateChunk, verifying<br />

our work with Figure 5–7. Note that mintChocolateChunk has its own mint member and inherits other<br />

members from MintChocolateChunk.prototype, Chocolate.prototype, and VanillaBean.prototype.<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 />

155

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

Saved successfully!

Ooh no, something went wrong!