JavaScript Scope Learning Notes

Source: Internet
Author: User

Previously only known to declare variables directly in the script tag, this variable is a global variable that can be accessed throughout the page (e.g. code-1). It has been a long time to let me think of confusion, in the case of more than the page JS, multiple files are likely to be re-affirmed in the introduction of the value of the variable, so there is such a study.

  Code-1:

<script>    var a = "1"; </script>

As the study went deeper, it was later discovered that the way in which the variables were declared in code-1, the actual scope was window, so the variables (functions) were extended to the object of window.

 Code-2:

var a = 1//  1

Later found that the original is called Execution context (execution environment), so there is the following meet.

For a friend with a C # or Java or other language background, the scope of the variable should look like this:

In which range the declaration ends in which range (a paired {).

  Code-3:

<script>    function  Test () {        if(true) {            var A=1;        }        Console.log (a); // undefined?     }

The result of the execution of the above code is presumably undefined. But is that really the case in JavaScript? After studying for such a long time, I feel that this is a programming language that can subvert the outlook on life, and can't trust intuition too much. Try it and find out that JavaScript is not seen in C # 's eyes.

  

In JavaScript, the variable declaration in the IF statement is added to the current execution environment. The execution environment in code 3 is the test function, so variable A is accessible throughout the test function. It is not destroyed until the function has been executed. And what is the execution environment of the function test? Oh, of course it is window.

This is important because the variable scope defined in JavaScript is not in a block, but in the execution environment (such as window, independent function).

  Code-4:

<script>    function  Test () {        function  testnew () {            var A = 1;                    }         // undefined     }
Testnew ()//error, testnew is not defined. </script>

The above code has a total of 3 execution environments, namely window, test, testnew. Although the Testnew execution environment is test, the variables declared in the testnew cannot be accessed in test. The variables declared in test cannot be accessed in the same window. It is important to note that, in addition to function, other block-level elements are not independent execution environments, such as: If,for,try, etc. So, hehe, variables declared in If,for,try can be accessed in the same execution environment.

Think about how "wonderful" JavaScript is for people (especially our C # programmers), where the scope is called the execution environment, and the universal fence brace is no longer omnipotent.

JavaScript Scope Learning Notes

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.