JavaScript_Succinctly
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
var bar = function () {<br />
var goo = function () {<br />
console.log(this); // Logs reference to head window object.<br />
} ();<br />
} ();<br />
} ();<br />
<br />
The simple concept here is that functions can be nested and there is no limit to how<br />
deep the nesting can go.<br />
Notes<br />
Remember, the value of this for nested functions will be the head object (e.g., the<br />
window object in a web browser) in <strong>JavaScript</strong> 1.5, ECMA-262, Edition 3.<br />
Passing functions to functions and returning functions from functions<br />
As previously mentioned, functions are first-class citizens in <strong>JavaScript</strong>. And since a<br />
function is a value, and a function can be passed any sort of value, a function can be<br />
passed to a function. Functions that take and/or return other functions are sometimes<br />
called "higher-order functions.”<br />
In the following code, we are passing an anonymous function to the foo function which<br />
we then immediately return from the foo function. It is this anonymous function that the<br />
variable bar points to, since foo accepts and then returns the anonymous function.<br />
Sample: sample95.html<br />
<br />
// Functions can be sent to, and sent back from, functions.<br />
var foo = function (f) {<br />
return f;<br />
}<br />
var bar = foo(function () { console.log('Hi'); });<br />
bar(); // Logs 'Hi'.<br />
<br />
So when bar is invoked, it invokes the anonymous function that was passed to the<br />
foo() function, which is then passed back from the foo() function and referenced from<br />
101