23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 6 ■ FUNCTIONS AND ARRAYS<br />

222<br />

Figure 6–17. Reworking the previous sample to use locally defined _bittersweet, _cocoa, and _vanilla<br />

variables rather than _bittersweet, _cocoa, and _vanilla named parameters<br />

Finally, note that, rather than wrapping the self-invocation in parentheses, you will often see the<br />

function expression wrapped in parentheses, which are then followed by the () operator as in the<br />

following sample. Try it, verifying your work with Figure 6–18:<br />

var ChocolateChocolate = null;<br />

(function (_bittersweet, _cocoa, _vanilla) {<br />

ChocolateChocolate = function (bittersweet, cocoa, vanilla) {<br />

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

this.cocoa = [3, "tbs", cocoa || _cocoa];<br />

this.vanilla = [1, "bean", vanilla || _vanilla];<br />

};<br />

})("Ghirardelli", "Callebaut", "Madagascar Bourbon");<br />

ChocolateChocolate.prototype = {<br />

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

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

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

yolks: [6]<br />

};<br />

var chocolateChocolate = new ChocolateChocolate("Lindt");<br />

console.dir(chocolateChocolate);<br />

ChocolateChocolate.toString();

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

Saved successfully!

Ooh no, something went wrong!