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

Chapter 7: Function Expressions > Mimicking Block Scope

MIMICKING BLOCK SCOPE

As mentioned previously, JavaScript has no concept of block-level scoping, meaning variables defined inside of block statements are actually created in the containing function, not within the statement. Consider the following:

image
function outputNumbers(count){
    for (var i=0; i < count; i++){
        alert(i);
    }
                   
    alert(i);   //count
}

BlockScopeExample01.htm

In this function, a for loop is defined and the variable i is initialized to be equal to 0. For languages such as Java and C++, the variable i would be defined only in the block statement representing the for loop, so the variable would be destroyed as soon as the loop completed. However, in JavaScript the variable i is defined as part of the outputNumbers() activation object, meaning it is accessible inside the function from that point on. Even the following errant redeclaration of the variable won’t wipe out its value:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

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

  

Start a Free Trial


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