The code for these 2 is the third version of the Advanced program. Personally think that the senior senior interpretation is not very deep and not explained clearly. So I followed the ECMA new explanation.
function Createfunctions () {
var result = new Array ();
for (var i=0; i <; i++) {
Result[i] = function (num) {//Creates a variable object scope chain that contains all the parent execution environment, first enters the execution environment of the function, the Num property value in the active object is 0, and the second enters the function's execution environment, The execution environment for each function entered is a new execution environment. It is clear that the execution environment is different for two times. Therefore, the value of the NUM attribute in the activity is 1;
return function () {//Creates a variable object scope chain containing all the parent execution environments when the function is created, and Num is 0 when entering the execution environment of the result[0] () function when the second active object of his scope chain is the parent function execution Environment's active object value is 0; when entering RESULT[1] () The second time the new execution environment of the function is entered, the second active object of the scope chain is the active object of the parent function execution Environment, note: The execution environment of the parent function at this time is a new execution environment, so its scope chain of the second active object pointer to the new parent function execution environment of the active object and a little bit. When the return function exits, the parent execution environment is destroyed, but the active object remains in memory, and the second active object pointer of the scope chain of the execution environment of the function that is saved to the result array refers to the active object of a new parent execution environment ; So num value is 1;
return num;
};
} (i);//When the loop body has finished executing. Each function object inside the result[i] is not equal.
}
return result;
}
function Createfunctions () {
var result = new Array ();
for (var i=0; i <; i++) {
Result[i] = function () {//Create a function when creating a variable object scope chain containing all the parent execution environment,
return i;
};
}
return result; ///Result[i] Each function object inside is not equal. when the loop body ends, the property of the active object of the execution environment of the createfunctions function i=10; the parent function execution environment is only entered once. Result[i] () each entry into the function execution environment is a new execution environment. The second active object pointer to the scope chain of the execution environment at this point is the active object that points to the same function's parent execution environment, so the value of I is ten;
}
The above views are all personal views. Do not like to see;
A deep understanding of the execution environment