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 5 ■ MEMBER INHERITANCE<br />

170<br />

var extend = (function () {<br />

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

return function (child, parent) {<br />

Proxy.prototype = parent.prototype;<br />

child.prototype = new Proxy();<br />

child.prototype.constructor = child;<br />

child.donor = parent.prototype;<br />

}<br />

}());<br />

var Cherry = function(cherry) {<br />

this.cherries = [2, "cup, pitted and halved", cherry ? cherry : "Bing"];<br />

};<br />

Cherry.prototype = {<br />

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

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

sugar: [9/16, "cup"],<br />

yolks: [3],<br />

vanilla: [1, "bean", "Madagascar Bourbon"]<br />

};<br />

var CherryGarcia = function(cherry, bittersweet) {<br />

Cherry.apply(this, [cherry]);<br />

this.bittersweet = [1, "cup, coarsely chopped", bittersweet ? bittersweet : "Callebaut"];<br />

};<br />

extend(CherryGarcia, Cherry);<br />

var cherry = new Cherry();<br />

var cherryGarcia = new CherryGarcia();<br />

console.dir(cherry);<br />

console.dir(cherryGarcia);

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

Saved successfully!

Ooh no, something went wrong!