Javascript scope chain

Source: Internet
Author: User

Fragment 1: Output 0123456789100

function test(){         var i = 0;        if(1==1){            var j = 0;                                 for(var k=0; k < 10; k++){                document.write(k);            }            document.write(k);                    }        document.write(j);                    }test();

Segment 2: Output undefined

function test(){         var i = 0;        if(1==2){            var j = 0;                                 for(var k=0; k < 10; k++){                document.write(k);            }            document.write(k);                    }        document.write(j);                    }test();


Based on the above understanding, describe the JS initialization process:

Create a global object before the JS interpreter executes any code

Use predefined values and functions to initialize attributes in the global object, such as eg. Math, Infinity, and parseInt.
Search for the var declaration outside the function, create the corresponding attribute of the global object, and initialize it as undefined.
Create a global execution environment. The scope chain has only one object-Global object.

Execute Code in sequence

Assign a value to the corresponding attribute of the global object in the var declaration assignment statement.
When a value assignment statement is not declared, add the corresponding attribute to the Global Object and assign a value to it.

When a function call occurs, create a call object.

Search for the var statements and parameters in the function, create the corresponding properties of the call object, and initialize them as undefined.
Create a function execution environment, scope chain -- first object: Call object; second object: Global Object
Execute Code in sequence
When the var declaration assignment statement is used to assign values to the properties of the called object
When a value assignment statement is not declared, add the corresponding attribute to the Global Object and assign a value to it.
When a function call occurs, create a nested function call object.
Search for the var statements and parameters in the nested function, create the corresponding attributes of the calling object of the nested function, and initialize it as undefined.
Create a nested function execution environment, scope chain -- first object: Call object of nested function; second object: Call object; third object: Global Object

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.