JavaScript Scope Principle (iii) -- scope is divided by function

Source: Internet
Author: User

JavaScript Scope Principle (iii) -- scope is divided by function
1. Copy the code var pscope3 = document to a for instance <p id = "scope3" style = "color: red"> </p>. getElementById ('scope3'); function scope3 () {for (var I = 0; I <10; I ++) {} echo (pscope3, I );} scope3 (); copy Code 1. in java, C #, and other languages, the variable I will only be defined in the for loop statement. When the loop ends, I will be destroyed. However, in JavaScript, 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: copy the code function scope3 () {var I; for (I = 0; I <10; I ++) {} echo (pscope3, i);} copy code 2. Private-scope anonymous function copy code function anonymous () {var position = 'in anonymous'; (function () {for (var I = 0; I <10; I ++) {}echo (pscope3, position); // print the display "in anonymous"}) (); // pscope3.innerHTML + = I; // error} anonymous (); copy Code 1. anonymous functions can be used to simulate block-level scopes to avoid the problem above. 2. A private scope is inserted outside the for loop. Any variables defined in anonymous functions will be destroyed at the end of execution. 3. If the above sentence is commented out, the error message "ReferenceError: I is not defined" will be displayed if the comment is canceled ". 4. the scope relationship is roughly as follows: 5. position is defined in the anonymous function, but can be printed out in the anonymous function, because the scope of anonymous contains anonymous functions, if the position definition cannot be found in the anonymous function, you can find it. Iii. Copy the code function capture () {var ex = 'in capture '; try {I;} catch (ex) in special cases in the catch part of the try statement) {var position = 'in catch '; echo (pscope3, ex); // ReferenceError: I is not defined var ex = 'is catching'; echo (pscope3, position ); // in catch echo (pscope3, ex); // is catching} echo (pscope3, position); // in catch echo (pscope3, ex ); // in capture is not the echo (pscope3, window. ex); // undefined} capture (); copy code 1, cat A parameter ex in ch has the same name as the local variable ex in the capture function. 2. Print ex First in catch, which is the error message, and then assign it to "is catching" to print it out. However, it is strange that the ex printed outside catch is "in capture ", it is not the value covered in it. 3. position is defined in catch, but can be printed out of catch. 4. window. ex outputs undefined, that is to say, ex is not global. Therefore, we can introduce the braces following catch as the nature of common statement blocks. 5. The nature of ex can be regarded as the only variable that regards the catch block as the block scope and is the local variable of the catch Block.

Related Article

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.