Fragment 1: Output 0123456789100 functiontest () {vari0; if (11) {varj0;
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