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.

Figure 6–18. Moving the () operator outside the parentheses works fine, too.<br />

Passing a Configuration Object<br />

CHAPTER 6 ■ FUNCTIONS AND ARRAYS<br />

Often, if you have a number of optional parameters, such as in our closure samples, you will want to<br />

pass a configuration object rather than separate parameters. Doing so prevents your having to pass "" or<br />

some other falsy value parameters prior to the one you want to explicitly pass as in the following sample:<br />

var ChocolateChocolate = function () {<br />

var _bittersweet = "Ghirardelli",<br />

_cocoa = "Callebaut",<br />

_vanilla = "Madagascar Bourbon";<br />

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

}();<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("", "", "Tahitian");<br />

console.dir(chocolateChocolate);<br />

So here’s how we can remedy that bugaboo by defining one parameter named pref. As Figure 6–19<br />

displays, this prevents our having to pass empty strings for bittersweet and cocoa in order to pass<br />

“Tahitian” for vanilla.<br />

223

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

Saved successfully!

Ooh no, something went wrong!