definition
apply: Call the function and replace the function's this value with the specified object, and replace the function's parameters with the specified array.
Syntax: apply([thisObj[,argArray]])
thisObj
Optional. Object to be used as this object.
argArray
Optional. A set of parameters to be passed to the function.
definition
call: Call an object's method and replace the current object with another object.
Syntax: call([thisObj[, arg1[, arg2[, [, argN]]]]])
thisObj
Optional. The object to be used as the current object.
arg1, arg2, , argN
Optional. The parameter list will be passed to the method.
3. The difference between the two
The second parameter of call can be of any type, while the second parameter of apply must be an array or arguments.
There are also differences in definitions.
4. Example analysis
(1) Official example:
function callMe(arg1, arg2){ var s = ""; s += "this value: " + this; s += "<br />"; for (i in ) { s += "arguments: " + [i]; s += "<br />"; } return s; } ("Original function: <br/>"); (callMe(1, 2)); ("<br/>"); ("Function called with apply: <br/>"); ((3, [ 4, 5 ])); ((3, 4, 5 )); // Output: // Original function: // this value: [object Window] // arguments: 1 // arguments: 2 // Function called with apply: // this value: 3 // arguments: 4 // arguments: 5
The first one uses apply: define: call the function and replace the function's this value with the specified object.
Call the function callMe and replace this in the callMe function with the specified object 3. At this time, this here changes from the previous [object Window] to 3.
The first one uses call: definition: call an object's method and replace the current object with another object.
Call the method of the object callMe and replace the object in callMe with another object 3.
From the analysis of these results, it can be seen that both of these use objects in the specified object or the specified value to change this in the object.
It can also be said that an object in a function (this) "hijacks" another object in a function (this) to be executed.
In fact, this raises a question: What exactly is this? Why is it so important to try hard to change its direction again and again?
(2) Example:
function zqz(a,b){ return alert(a+b); } function zqz_1(a,b){ (zqz_1,[a,b]) } zqz_1(1,2) //->3
Analysis: By definition: call the function and replace the function's this value with the specified object,
Here the function zqz is called, and the specified object zqz_1 is replaced by this value of the zqz function.
Note that the function name in js is actually an object, because the function name is a reference to the Function object!
function add(a, b) { alert(a + b); } function sub(a, b) { alert(a - b); } (sub, 3, 1); // 4
Analysis: By definition: Call the method of one object and replace the current object with another object.
Here is the method of calling the object add and replacing the current object sub with the add object;
Let’s take another example:
function zqz(a,b){ =a; =b; alert(+" "+); } function zqz_1(a,b){ (this,[a,b]) //We can also write this (this, arguments)} zqz_1("Nic",12) //Nic 12
Analysis: By definition: call the function and replace the function's this value with the specified object,
Here, the function zqz is called, and the specified object this is replaced by the function zqz.
Let’s take another example:
<input type="text" value="input text"> function Obj(){ ="Object!"; } var value="global variable"; function Fun1(){ alert(); } Fun1(); //global variable(window); //global variable(('myText')); //input text (new Obj()); //Object!Fun1(); //global variable
Analysis: Definition: Call an object's method and replace the current object with another object.
Call the method of the Fun1 object and replace the object in the current Fun1 with the window object.
Call the method of the Fun1 object and replace the object in the current Fun1 with the object in input.
Call the method of the Fun1 object and replace the object in the current Fun1 with the object in the new obj that comes out.
Let’s take a look at the questions raised by a netizen:
The call method can be used to call a method instead of another object. The call method changes the object context of a function from the initial context to a new object specified by thisObj. If the thisObj parameter is not provided, the Global object is used as thisObj.
Then I wrote a case by myself, and the one I wrote was different from what I imagined; the following code
function parent() { alert(); } function child() { var name = 'Zhang San'; }; (child);
What he outputs is child. Why isn't Zhang San? According to the above sentence, the parent context has become child.
And there is a name value in child. The one that should be output is Zhang San. Please explain to the master.
function parent() { alert(); } function child() { = 'Zhang San'; }; var p1 = new child(); (p1);
This way, you can output Zhang San. Why?
What's going on? Let's take a look
Call and apply have a useful function, which is that they can be called with variables as function names. For example, a callback function of a function. The specific usage is: the executed function.call(a,b,c...), where a is the object that needs to be specified in the executed function, which can be null, and other parameters are parameters of the executed function. The use of apply is similar, except that the second parameter is an array.
Give an example:
function doPost(url,param,callback){ //The post request is processed here var str = ; (this,[str]);//Equivalent to calling callback(str); and setting this in callback as a doPost object}