04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4: Variables, Scope, and Memory<br />

In this figure, the rectangles represent specific execution contexts. An inner context can access everything<br />

from all outer contexts through the scope chain, but the outer contexts cannot access anything within an<br />

inner context. The connection between the contexts is linear and ordered. Each context can search up the<br />

scope chain for variables and functions, but no context can search down the scope chain into another<br />

execution context. There are three objects in the scope chain for the local context of swapColors(): the<br />

swapColors() variable object, the variable object from changeColor(), and the global variable object.<br />

The local context of swapColors() begins its search for variable and function names in its own variable<br />

object before moving along the chain. The scope chain for the changeColor() context has only two objects:<br />

its own variable object and the global variable object. This means that it cannot access the context of<br />

swapColors() .<br />

Function arguments are considered to be variables and follow the same access rules as any other variable<br />

in the execution context.<br />

Scope Chain Augmentation<br />

Even though there are only two types of execution contexts, global and local (function), there are other<br />

ways to augment the scope chain. Certain statements cause a temporary addition to the front of the<br />

scope chain that is later removed after code execution. There are two times when this occurs, specifically<br />

when execution enters either of the following:<br />

❑<br />

❑<br />

The catch block in a try - catch statement<br />

A with statement<br />

Both of these statements add a variable object to the front of the scope chain. For the with statement, the<br />

variable object contains variable declarations for all properties and methods of the specified object; for<br />

the catch statement, the variable object contains a declaration for the thrown error object. These variable<br />

objects are read - only, so variables declared in a with or catch statement are added to the execution<br />

context ’ s variable object. Consider the following:<br />

function buildUrl() {<br />

var qs = “?debug=true”;<br />

with(location){<br />

var url = href + qs;<br />

}<br />

}<br />

return url;<br />

In this example, the with statement is acting on the location object, so a variable object containing all<br />

of location ’ s properties and methods is added to the front of the scope chain. There is one variable, qs ,<br />

defined in the buildUrl() function. When the variable href is referenced, it ’ s actually referring to<br />

location.href , which is in its own variable object. When the variable qs is referenced, it ’ s referring to<br />

the variable defined in buildUrl() , which is in the function context ’ s variable object. Inside the with<br />

statement is a variable declaration for url . Because this variable object is read - only, url becomes part of<br />

the function ’ s context and can, therefore, be returned as the function value.<br />

87

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

Saved successfully!

Ooh no, something went wrong!