var i = 2,
x = 5;
var fn = function (x) {
x + = 3;
return function (y) {
Console.log (x + +) + y + (--);
}
};
var f = fn (1);
F (2);
FN (3) (4);
F (5);
Answer: F (2) = 7; FN (3) (4) = 10; F (5) =>9;
Problem Solving Ideas:
1. var f = fn (1); equivalent to opening a heap of memory (private scope), parameter assignment. Top-down execution, x assignment is 4;
2. F (2) performs a return function (y) parameter assignment, function (y) opens up a heap of memory, X does not assign a value in its own scope through the scope chain to find the superior x assignment to 4,i also find global scope through the scope chain
I is assigned a value of 2, the operation (x + +) +y+ (-i) The result is a 7 operation, and the value of the parameter at this time is 5, and the variable I under the global scope is 1.
3. FN (3) (4) performs a re-opening of a heap memory (private scope), parameter assignment. X is 3,y to 4. Because the last operation makes the I assignment of the global scope to 1. Therefore, after the top-down execution (x + +), the result of +y+ (-i) is 10, and the variable I under the global scope is assigned a value of 0.
4.F (5) performs the same parameter assignment as F (2), but it is important to note that after a top-down operation, the X assignment is 5,i assignment to 0, and (x + +) The result of the +y+ (-i) is 9; At this point the value of the parameter is 6, and the variable I under the global scope is negative 1
5. Attach a detailed problem solving map!!!!
JS on "Variable promotion, scope, private scope and other knowledge points" advanced problem-solving ideas