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

228<br />

Using the first way, let’s paste in our callback sample from earlier in the chapter. As Figure 6–22 and<br />

6–23 display, we can invoke clone() from within the module but not outside of it for the reason that<br />

clone() and SweetCream() are not globally defined. So JavaScript returns a reference error in Figure 6–23.<br />

(function () {<br />

var clone = typeof Object.create === "function" ?<br />

function (donor) {<br />

return typeof donor !== "function" ?<br />

Object.create(donor) :<br />

Object.create(new donor());<br />

} :<br />

function (donor) {<br />

var Proxy = function () {};<br />

Proxy.prototype = typeof donor !== "function" ?<br />

donor :<br />

new donor();<br />

return new Proxy();<br />

};<br />

var SweetCream = function () {};<br />

SweetCream.prototype = {<br />

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

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

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

yolks: [6]<br />

};<br />

console.dir(clone(SweetCream));<br />

}());<br />

Figure 6–22. Calling clone() within the module works

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

Saved successfully!

Ooh no, something went wrong!