1. Related question Description: How to understand the closure? Code Snippet A:
! function() { var num=1; var exp=return num++;} exp.getaddnum=functionreturn add (num);} window.a=// 1console.log (A.getaddnum ()); 1
Code Snippet B:
! function() { var num=1; var exp=return num++;} exp.getaddnum=functionreturn add ();} window.a=// 1console.log (A.getaddnum ()); 2
Explanation: The first inside is what you pass in, and he will use the value of the parameter received by the current scope, and it does not change the value of the outer num, so each time you use it to pass, the value is 1.
And the second ' add ' method does not have a num variable, and he will find the outer num through the scope chain, so you call it every time the value of the outer variable of the operation, and this value in your
returnAfter that, it will accumulate. About context and scope chain you can read this article
Graphical JavaScript context and scope: http://blog.rainy.im/2015/07/04/scope-chain-and-prototype-chain-in-js/
2.javascript face question about closures
Functionf1 () { var n=999; nadd=function () {n+=1} functionf2 () {alert (n); } return F2; } var result=// 999//
Ask:
In this code, result () it runs two times, the first value is 999, and the second value is 1000. This proves that the local variable n in the function F1 is kept in memory and is not automatically cleared after the F1 call. Why is that? Especially the second time, why is the output not 999?
What does Nadd=function () {n+=1} do?
Explain:
First, the scope chain of the F1 function. 1 points to global, 2 points to itself. itself includes, n=999 F2, nadd This function does not add VAR declaration, is a global variable. But the inside of N refers to the n=999 inside the F1.
Then f2 the scope chain of the function. 1 point to Global 2 to F1 scope 3, point to own scope
Nadd This scope chain 1 points to global, 2 holds n=999 reference, (I think also points to F1 scope), 3 points to own scope
Execute var result=f1 (); This landlord should be clear, is the result is pointing to the F2 function
Call result (); pop n. Just go to your own scope first. No. And then go to the higher-level scope F1 to find. Got it. n=999, pop up 999
Execute Nadd (), then, go to your own scope to find. No n, so go to superior scope. In the F1. Find N, perform n=n+1,n change 1000
Execute result again (); same as last time. is to first go to their own scopes to find. No. And then go to the higher-level scope F1 to find. When I found it, n=1000. So pop up 1000
3.[] and Array calls the slice method causes the problem
The problem is: in some scenarios, the function's arguments parameter needs to be called as an array, but arguments is a singular object, so try to convert arguments into an array;
Functionargtoarr () { return [].slice.call (arguments, 0);} Console.log (Argtoarr ()); //[Functionargtoarr]() {returnArray.slice.call (arguments, 0//[]
Ask:
What is this for?
There is also a question about how Array finds the slice method.
The array itself is not slice method, its method in Array.prototype, and when we call the slice method, if the array itself does not find the slice method, it will be found through its prototype chain, and Array.proto does not Point to Array.prototype, but to Function (), then how does it find the slice method?
Explain:
The second piece of code error is because Array it is a constructor, not an object, open the console, enter typeof Array , the result isfunction
You also said that the slice() method in its prototype object, but [] Array the prototype object, in the console input Array.prototype , the result is [] , so the first piece of code can be executed smoothly.
The second code can be modified as follows:
Functionargtoarr () { // Change this line }console.log (Argtoarr ( ));
In fact, your essential problem is that you are mistaken for Array an array object, but it is a constructor.
Interesting JavaScript.