Mimic block-level scopes

Source: Internet
Author: User

There is no concept of block-level scope in JavaScript. This means that the variables defined in the block statement are actually created in the containing function rather than in the statement.

<Script> for(varI=0; I<5; I++) {}alert (i);</Script>

In the above example, you can eject I is 5, because the for statement is actually contained in the function () {} of window.onload= functions, and alert is also in this function.

In Java, C + + I will only be defined in the statement block for the For loop, and the loop will be destroyed as soon as it ends. In JavaScript I is the active object defined in XXX (), so you can access him anywhere within the function, starting with his definition.

<Script> for(varI=0; I<5; I++){}varI;alert (i);</Script>

Even if the same variable is incorrectly re-declared as above, it will not change his value by 5. JavaScript never tells you if you have declared the same variable many times. In this case, it will only turn a blind eye to the subsequent declaration, but he will perform the variable initialization in the later declaration. If the above code is changed to Var i=3, then the last alert is 3.

< Count ; i++) {alert (i);}}) (); alert (i); Cause an Error! }

This example is to define a private scope outside the for loop so that the variables defined in the anonymous function are destroyed when they are finished. This anonymous function is a closed packet.

This technique is often used outside the function in the global scope to limit the addition of too many variables and functions to the global scope.

In general, we should always add variables and functions to the global scope as little as possible . In a large number of developers who are involved
In applications, too many global variables and functions can easily lead to naming conflicts. by creating a private scope, each developer can
to use your own variables without worrying about messing up the global scope . For example:

(function () {var now = new Date (), if (now.getmonth () = = 0 && now.getdate () = = 1) {alert ("Happy new year!");}) ();  
Put this code in the global scope so that it pops up on January 1 ..., but if you don't do this, then you need to declare a variable now in the global scope.

This approach reduces the memory problems that are used by closures because there are no references to anonymous functions. As long as the letter
Once the number has been executed, its scope chain can be destroyed immediately.

Mimic block-level scopes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.