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

218<br />

Let’s get to the JavaScript to see what we can do with all this information. First, some background:<br />

any variable that you declare within the curly braces wrapping the block of an if, for, while, or other<br />

compound statement is visible outside the block, as you have seen throughout this book. Put another<br />

way, curly braces do not provide a way to create private variables.<br />

In contrast, functions have function scope, which means that any variables or functions declared<br />

within the curly braces wrapping a function block normally are not visible outside of the block. Let’s find<br />

out why.<br />

Whenever you define a function, JavaScript saves the scope chain as part of the new function object;<br />

that is to say, the scope chain is the sequence of objects, beginning with the function’s own call object<br />

and ending with the global window object. This means this part of the scope chain is set in stone before<br />

the function ever runs. However, any variables, functions, or arguments contained by the call and global<br />

objects comprising that scope chain are live.<br />

Then, when you invoke a function, JavaScript adds any variables defined locally within the function,<br />

named parameters, the arguments object, and this to the scope chain. So these variable objects will<br />

differ every time you invoke the function. JavaScript looks in this complete set of items in the scope<br />

chain when it is looking up the value of a variable. In other words, when you invoke a function that uses<br />

a variable, JavaScript can only use the variable if it is declared in the scope chain.<br />

Normally, after an invoked function returns, everything added to the scope chain when you invoked<br />

the function is destroyed. However, if you create a closure, the objects contained in function scope are<br />

not destroyed. Therefore, you may query the named parameters and locally defined variables for that<br />

invocation even after it has ended. Let’s look at closures now.<br />

Insofar as closure is a wildly popular technique, do yourself a favor and go ten toes in while we<br />

explore those now. Say we want to save some default values for bittersweet chocolate, cocoa, and vanilla<br />

to a closure our ChocolateChocolate() constructor can query. One way would be to define those as local<br />

variables for a self-invoking function and then have its return value be the ChocolateChocolate()<br />

constructor.<br />

So, in the following sample, chocolateChocolate is churned with Callebaut cocoa and Madagascar<br />

Bourbon vanilla due to closure, as Figure 6–14 displays, along with the function JavaScript assigns to<br />

ChocolateChocolate.<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("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!