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 6 ■ FUNCTIONS AND ARRAYS<br />

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

child;<br />

Proxy.prototype = parent;<br />

child = new Proxy();<br />

if (descriptors !== undefined) {<br />

Object.defineProperties(child, descriptors);<br />

}<br />

return child;<br />

};<br />

}<br />

Function.prototype.bind = Function.prototype.bind ||<br />

function (obj) {<br />

var that = this,<br />

ossify = [].slice.call(arguments, 1),<br />

fn = function () {<br />

return that.apply(this instanceof that ? this : obj,<br />

ossify.concat([].slice.call(arguments, 0)));<br />

};<br />

fn.prototype = Object.create(that.prototype);<br />

return fn;<br />

};<br />

Now for the moment of truth. Let’s try currying a constructor that churns Wild Maine Blueberry ice<br />

cream with its blueberries and lemon parameters. Insofar as we’re presetting those parameters to their<br />

typical winter values—no fresh wild blueberries to be had now—we’ll name the new constructor<br />

WinterWildMaineBlueberry. Then since WinterWildMaineBlueberry just takes a vanilla parameter—the<br />

blueberries and lemon parameters are preset—we’ll just pass "Tahitian" to choose a mild vanilla bean,<br />

verifying our work with Figure 6–11:<br />

if (Object.defineProperties === undefined) {<br />

Object.defineProperties = function (obj, descriptors) {<br />

for (descriptor in descriptors) {<br />

if (descriptors.hasOwnProperty(descriptor)) {<br />

obj[descriptor] = descriptors[descriptor].value;<br />

}<br />

}<br />

};<br />

}<br />

if (Object.create === undefined) {<br />

Object.create = function (parent, descriptors) {<br />

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

child;<br />

Proxy.prototype = parent;<br />

child = new Proxy();<br />

if (descriptors !== undefined) {<br />

Object.defineProperties(child, descriptors);<br />

}<br />

return child;<br />

};<br />

}<br />

Function.prototype.bind = Function.prototype.bind ||<br />

function (obj) {<br />

var that = this,<br />

ossify = [].slice.call(arguments, 1),<br />

211

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

Saved successfully!

Ooh no, something went wrong!