There is a call and apply method in JavaScript, which is basically the same, but it has a slightly different effect.
Let's take a look at the explanation of call in the JS manual:
Call method
Invokes one method of an object, replacing the current object with another object.
Call ([thisobj[,arg1[, arg2[, [,. ArgN]]])
Parameters
Thisobj
Options are available. The object that will be used as the current object.
Arg1, Arg2,, ArgN
Options are available. The method parameter sequence will be passed.
Description
The call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial context to a new object specified by Thisobj.
If the Thisobj parameter is not provided, then the Global object is used as the thisobj.
To be clear is actually to change the object's internal pointer, that is, to change the object's this point to the content. This is sometimes useful in the object-oriented JS programming process.
Quoting a code snippet on the web, it is natural to understand it after running.
<input type= "text" id= "MyText" value= "input text" >
<script>
function Obj () {this.value= "Object! ";}
var value= "global variable";
function Fun1 () {alert (this.value);}
Window. Fun1 (); Global variables
Fun1.call (window); Global variables
Fun1.call (document.getElementById (' MyText ')); Input text
Fun1.call (New OBJ ()); Object!
</script>
The first parameter of the call function and the Apply method is the object to pass to the current object, and this inside the function. The arguments that follow are all arguments passed to the current object.
Run the following code:
<script>
var func=new function () {this.a= "func"}
var myfunc=function (x) {
var a= "MyFunc";
alert (THIS.A);
alert (x);
}
Myfunc.call (func, "Var");
</script>
It is visible that Func and Var are ejected separately. Here you get a sense of the meaning of each of the call's parameters.
Both apply and call are the same in effect, but they differ in parameters.
The meaning for the first parameter is the same, but for the second argument:
Apply passes in an array of parameters, which is the combination of multiple parameters into an array, and call is passed in as a call parameter (starting with the second argument).
such as Func.call (FUNC1,VAR1,VAR2,VAR3) corresponding to the wording of the Apply: Func.apply (Func1,[var1,var2,var3])
The benefit of using apply at the same time is that you can directly pass in the arguments object of the current function as the second parameter of apply
[Translated from: http://www.cnitblog.com/yemoo/archive/2007/11/30/37070.html]
On the usage and difference of application and call in JavaScript