Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

4. Function() > Functions Can Be Nested

Functions Can Be Nested

Functions can be nested inside of other functions indefinitely. Below, we encapsulate the goo function inside of the bar function, which is inside of the foo function.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var foo = function() {
   var bar = function() {
       var goo = function() {
          console.log(this); // logs reference to head window object
       }();
   }();
}();

</script></body></html>

The simple takeaway here is that functions can be nested and that there is no limit to how deep the nesting can go.

Note

Remember, the value of this for nested functions will be the head object (e.g., window object in a web browser) in JavaScript 1.5, ECMAScript 3 Edition.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint