Today encountered a problem, which contains both the knowledge of anonymous functions, but also includes pre-compilation, function of the parameters (formal parameters), feeling vaguely, so think about doing a summary.
var foo={n:1}; (function(foo) { console.log (FOO.N); FOO.N=3; var foo={n:2}; Console.log (FOO.N); }) (foo); Console.log (FOO.N);
The above code can be written like this, look at the explanation
1 varFoo={n:1};2(function(foo) {//The parameter and the argument point to the same memory space, and the value N of the space is 13 varFoo//The elevation of a variable, the declaration priority of a variable is lower than the formal parameter, so this line is equivalent to invalid4Console.log (FOO.N);//This should output 15foo.n=3;//The memory space where the parameters and arguments are pointing is changed, and the value now becomes 36 varFoo={n:2};//This line is critical, opening up a new memory space, with a value of N of 27Console.log (FOO.N);//since the code in JS is executed from top to bottom, output 2 at this time8 }) (foo);9Console.log (FOO.N);//the previous memory space value is 3, so the output 3
Result Output 1 2 3
According to the above idea, if line fifth and line sixth change.
1 varFoo={n:1};2(function(foo) {//The parameter and the argument point to the same memory space, and the value N of the space is 13 varFoo//The elevation of a variable, the declaration priority of a variable is lower than the formal parameter, so this line is equivalent to invalid4Console.log (FOO.N);//This should output 15 varFoo={n:2};//This line is critical, opening up new memory space and previously disconnected, with a value of N of 26foo.n=3;//changes the value in the new memory space, and the value now changes to 3 .7Console.log (FOO.N);//since the code in JS is executed from top to bottom, output 3 at this time8 }) (foo);9Console.log (FOO.N);//the previous memory space value is 1, so the output 1
Result Output 1 3 1
OK, then what about this?
1 varFoo={n:1};2(function(foo) {//The parameter and the argument point to the same memory space, and the value N of the space is 13 varFoo//The elevation of a variable, the declaration priority of a variable is lower than the formal parameter, so this line is equivalent to invalid4Console.log (FOO.N);//This should output 15foo.n=3;//The memory space where the parameters and arguments are pointing is changed, and the value now becomes 36foo.n=2//and did not open up a new memory space, modified on the original basis, the value of n is 27Console.log (FOO.N);//since the code in JS is executed from top to bottom, output 2 at this time8 }) (foo);9Console.log (FOO.N);//OriginalThe memory space value is changed to 2, so the output 2
Result Output 1 2 2
The bottom is the same as above.
1 varFoo={n:1};2(function(foo) {//The parameter and the argument point to the same memory space, and the value N of the space is 13 varFoo//The elevation of a variable, the declaration priority of a variable is lower than the formal parameter, so this line is equivalent to invalid4Console.log (FOO.N);//This should output 15foo.n=2;//The memory space where the parameters and arguments are pointing is changed, and the value now becomes 26foo.n=3;//and did not open up a new memory space, modified on the original basis, the value of n is 37Console.log (FOO.N);//since the code in JS is executed from top to bottom, output 3 at this time8 }) (foo);9Console.log (FOO.N);///The original memory space value has been modified to changeis 3, so the output 3
Result Output 1 3 3
What about the bottom one? (lines fifth and sixth change direction, because they are not modified on the original basis, so the last value entered does not change)
1 varFoo={n:1};2(function(foo) {//The parameter and the argument point to the same memory space, and the value N of the space is 13 varFoo//The elevation of a variable, the declaration priority of a variable is lower than the formal parameter, so this line is equivalent to invalid4Console.log (FOO.N);//This should output 15 varFoo={n:2};//opens up new memory space with a value of N of 26 varFoo={n:3};//opens up new memory space with a value of N of 37Console.log (FOO.N);//since the code in JS is executed from top to bottom, output 3 at this time8 }) (foo);9Console.log (FOO.N);The value of the original memory space is1, and has not been modified, so output 1
Result Output 1 3 1
It's just like the top one.
1 varFoo={n:1};2(function(foo) {//The parameter and the argument point to the same memory space, and the value N of the space is 13 varFoo//The elevation of a variable, the declaration priority of a variable is lower than the formal parameter, so this line is equivalent to invalid4Console.log (FOO.N);//This should output 15 varFoo={n:3};//opens up new memory space with a value of N of 36 varFoo={n:2};//opens up new memory space with a value of N of 27Console.log (FOO.N);//since the code in JS is executed from top to bottom, output 2 at this time8 }) (foo);9Console.log (FOO.N);//The original memory space has not been modified, so output 1
Result Output 1 2 1
The above for the younger brother interpretation, if the fault also ask the great God advice.
anonymous function in JS