In JavaScript 1.2 and later, however, functions can be defined
anywhere, and tricky issues of scope arise. For example, consider a
function g defined within a function
f. g is always executed in the
scope of f. Its scope chain includes three
objects: its own call object, the call object of f(
), and the global object. Nested functions are perfectly
understandable when they are invoked in the same lexical scope in
which they are defined. For example, the following code does not do
anything particularly surprising:
var x = "global";
function f( ) {
var x = "local";
function g( ) { alert(x); }
g( );
}
f( ); // Calling this function displays "local"