In-depth understanding of variable scopes in JS

Source: Internet
Author: User

In JS, the scope (scope) of a variable is the area in the program that defines the variable. The variables are divided into two categories: global and Local. Where global variables are scoped globally, that is, in JavaScript code, it is defined everywhere. Variables declared within a function are defined only within the body of the function. They are local variables and the scope is local. The parameters of the function are also local variables, which are defined only in the body of the function.

We can better understand the scope of variables with JavaScript's scope chain (scope chain). Each JavaScript execution environment has a chain of scopes associated with it. This scope chain is an object list or chain of objects. When the JavaScript code needs to query the value of the variable x (a process called variable resolution (variable name resolution)), it begins to look at the first object of the chain. If the object has a property named X, then the value of that property is used. If the first object does not have a property named X, JavaScript continues to query the second object in the chain. If the second object still does not have a property named X, continue querying the next object, and so on. If the query does not exist at the end (in the top-level code), then the value of the variable is undefined.

The above process is not a familiar sequence structure, but roughly similar to a sequential structure is just a matter of looking at the scope as a whole. The whole process is as shown

JS Scope instance

Code one (plain can no longer dull code)

[JavaScript]View Plaincopyprint?
    1. var i=10;
    2. function A () {
    3. alert (i);
    4. };
    5. A ();

Code two

[JavaScript]View Plaincopyprint?
    1. var i=10;
    2. function A () {
    3. alert (i);
    4. var i = 2;
    5. };
    6. A ();

According to the "Years" of programming experience you may think that the two code output is the same, but the fact is that the first code normally output the value of the variable----10, and the second code output is undefined. Maybe a lot of people can't understand it, but we understand it well according to the diagram of the previous scope chain.

A clear representation of the scope chain diagram is that in the process of variable parsing, the local scope is first looked up and then the upper scope is found. The variable i is not defined in the function of code one, so it looks for the upper scope (global scope) and then outputs its value. However, the variable i is defined within the Code II function (either after the alter or before the variable is defined in the scope of the variable i), so it is no longer looked up to the upper scope and outputs I directly. Unfortunately, the local variable i is not assigned at this time, so the output is undefined.

The " No block scope " proposed in the JavaScript authoritative guide is actually the above meaning. Many times think understand, understand, in fact, did not understand, thin study after looking back to the book that bold text, suddenly suddenly dawned, in fact, people in the book said quite clear!

In-depth understanding of variable scopes in JS

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.