JavaScript scope principle (c)--scope according to function division

Source: Internet
Author: User
Tags variable scope

One for instance
<id= "Scope3"  style= "color:red"></p >
var pscope3 = document.getElementById (' scope3 ');         function Scope3 () {            for (var i=0; i<10;i++) {                            }            Echo (Pscope3,  i);        }        Scope3 ();

1, the function in Java, C # and other languages, the variable I will only be defined in the FOR Loop statement, the end of the loop, I will be destroyed. In JavaScript, however, the variable i is defined in the Scope3 () activity object, so it can be accessed inside the function at the beginning of its definition.

2, the printed i is "10".

3. The function scope3 is equivalent to the following code:

function Scope3 () {            var i;              for (i=0; i<10; i++) {                            }            Echo (Pscope3,  i);        }        

anonymous function of private scope
function Anonymous () {            var position = ' in anonymous ';            (function() {                for (var i=0; i<10;i++) {                                }                Echo ( Pscope3, position); //Print show "in Anonymous"            }) ();             // pscope3.innerhtml + = i;//Error         }        Anonymous ();

1, anonymous function can be used to imitate the block-level scope, to avoid the above problem.

2. A private scope is inserted outside the For loop. Any variables defined in an anonymous function will be destroyed at the end of execution.

3. If the above sentence is commented out, uncomment it and the error message will be displayed: "REFERENCEERROR:I is not defined".

4, the scope of the relationship is generally as follows:

5,position is defined in the anonymous function, but can be printed in the anonymous function, because the scope of the anonymous to wrap the anonymous function, in the anonymous function cannot find position definition, go outside.

Third, the special case of the catch part of the try statement
functioncapture () {varex = ' in Capture '; Try{i; }Catch(ex) {varPosition= ' in Catch '; Echo (Pscope3, ex); //referenceerror:i is not defined                varex = ' is catching '; Echo (pscope3, position);//In catchEcho (Pscope3, ex);//is catching} echo (pscope3, position);//In catchEcho (Pscope3, ex);//in capture is not a value assigned in CatchEcho (Pscope3, Window.ex);//undefined} capture ();

1. A parameter ex of catch has the same name as the local variable ex under the capture function.

2, the catch in the first print ex, is an error message, and then assigned to "is catching", printed out, but very strange, catch outside the print ex is "in capture", not the value of the inside coverage.

3. Position is defined in catch, but can be printed out of the catch.

4, Window.ex output is undefined, that is, ex is not a global, so you can roll out the brace behind the catch is the nature of the ordinary statement block.

5. The nature of the ex can be regarded as the only variable that treats the Catch statement block as a block scope, and is the local variable of the catch statement block.

Demo Download:

http://download.csdn.net/detail/loneleaf1/7983577

Resources:

http://www.laruence.com/2009/05/28/863.html JavaScript Scope principle

http://www.cnblogs.com/lhb25/archive/2011/09/06/javascript-scope-chain.html JavaScript Development Advanced: Understanding JavaScript scopes and scope chains

http://www.cnblogs.com/zxj159/archive/2013/05/30/3107923.html JavaScript anonymous function (mimic block-level scope)

Http://www.web-tinker.com/article/20331.html "pseudo-block scope" for Try-catch statements

Http://msdn.microsoft.com/zh-cn/library/bzt2dkta%28v=vs.94%29.aspx variable scope (JavaScript)

Scope of http://www.cnblogs.com/rubylouvre/archive/2009/08/21/1551270.html JavaScript variables

http://www.nowamagic.net/librarys/veda/detail/896 JavaScript Variable Scope

Http://www.web-tinker.com/article/20331.html "pseudo-block scope" for Try-catch statements

JavaScript scope principle (c)-scope is divided by function

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.