Found some articles on call and apply the existence of misunderstanding, because the interpretation of misunderstanding is easier to understand it, of course, there are criticisms, but all just say how is right, no specific analysis of misunderstanding why is misunderstanding
Today, let's take a closer look at why it's inappropriate.
function Add (A, b) { + b);} function Sub (A, b) { -3, 1); // 4
1: Replace sub with add
The words 2:call the front of the method (add) are handed to the back (sub)
The 3:sub has already taken the Add method
Properly understood:
The context of the add execution is switched from the Window object to a reference to the sub, that is, this points to a reference to a sub from the Window object, which is nothing in fact.
The above is the point of view from the network to select
The "rhetoric" faction says it looks the same, so what is the right understanding?
OK, first of all, let's look at what the context is:
In JavaScript, your code will always have some form of context (the object in which the code works inside). The context is working through the variable this, and the variable this always refers to the object that the code is currently in. The global object is actually a property of the Window object, which means that even in the global context, the this variable still references an object.
To understand the context, let's look at a function like this
function Test () { var myvalue = 1; this. Value = 2;}
In this function, value is clearly present in the context
And myvalue is not accessible with this, like the concept of a private variable
The problem is, for the "rhetoric" faction of the understanding of the way, "private variables" is also passed, so the result is 4, no 2
Can you see a strange feeling here? Is that really the case?
Let's go over the analysis and rewrite the add and sub two functions:
function Add (A, b) { alert (inner (A, b));} function Sub (A, b) { function inner () { this. Value = arguments[0]-arguments[ 1]; return This . Value 3, 1);
The alert (inner (A, b)) is passed to the sub, and the result is 2, as explained in the "rhetoric".
In fact, when you run this code, you will find that the error "Inner is not defined"
The "rhetoric" faction may stand up to refute, add has all covered the sub, there must be no inner method
Let's go ahead and change the Add.
function Add (A, b) { alert (this);} function Sub (A, b) { function inner () { this. Value = arguments[0]-arguments[1<
c12>];
return
This
. Value
Then look at the output
Oh, buy a cake, this is God horse situation? How does the Sub function code body output?
"1" is completely wrong, the sub is replaced with add after how this is a sub?
"2" and "2" are also incorrect, if the add is a sub or sub to take the Add method, then the result of executing alert (this) in the sub, this point to the window
So, "the rhetoric" faction, the regiment extinguishes
Let's take a look at what happens to the output of the function code body itself?
function this. Value = 1;} Console.log(' Test: ', test); Console.log (' Test (): ', Test ()); Console.log (New
Output Result:
A reference to the output function to output the function code body itself
It is easy to explain the fact that the "Sub function code body output" phenomenon is "properly explained" by saying:
Call and apply implement the context of the Toggle function object
The context of add is switched to the sub reference, so this is the entire output of the sub function Code body, in fact, it doesn't make any sense at all, because This.value is undefined
Based on this conclusion, we changed the code at the beginning of this article so that the sub was involved in the work.
function Add (A, b) { + b); This (A, B) // This is equivalent to executing a sub }function Sub (A, b) { -3, 1); // 4 //2
Is it now clearer and more essential to recognize the role of call?
However, in daily development, we need to switch to a meaningful context, that is, this.value after switching above, it is convenient to take different values, we found that the new instance of the result is a {} object, the most consistent with our needs, like this
function Add (A, b) { this. Inner (A, b);} function Sub () { thisfunction(A, b) { - b);} ;} Add.call (New Sub (), 3, 1); // 2
OK, pull so much, finally, another example of more direct dry goods, to see a code
function Add (A, b) { this. Inner (A, b);} function Sub (A, b) { thisfunction() { alert (arguments.callee.caller); };} Add.call (New Sub (), 3, 1);
At this time Arguments.callee equivalent to This.inner, when This.inner () execution, Arguments.callee only effective
Once again, This.inner (A, B) is given a sub to execute, and the executor is a sub, then the caller of This.inner () must point to the sub
In fact, this is not the case, the output:
The result of the output is add, which means that call simply replaces the add context with a sub,performer is still add, not sub。
Reprint Please specify source: http://www.cnblogs.com/tevinli/p/4558107.html
Illustration of the misunderstanding of call and apply in JS