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:
The call method invokes one method of an object, replacing the current object with another object. Call ([thisobj[,arg1[, arg2[, [,. ArgN]]]) parameter thisobj Optional. The object that will be used as the current object. Arg1, Arg2, , argn options available. The method parameter sequence will be passed. Indicates that 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< /span> Obj () {this . Value= "Object! ";} var value= "global variable" < span> function Fun1 () {alert (this Span style= "color: #000000;" >.value);} Window. Fun1 (); // global variable fun1.call (window); // global variable 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=Newfunction() {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.
As shown in the following code:
functionprint (A, B, C, d) {Alert (A+ B + C +d);}functionexample (A, B, C, d) { //using the call method to borrow print, parameter explicit break-through transferPrint.call ( This, A, B, C, d); //use the Apply method to borrow print, the parameter is passed as an array, //here directly with the JavaScript method within the arguments array itselfprint.apply ( This, arguments); //or encapsulate an array ofprint.apply ( This, [A, B, C, d]);}//The "backlight script" is shown belowexample ("Back", "light", "foot", "Ben");
The last sentence is called the example method directly, so the context object in the method is the Window object.
So, call, apply methods except for the first argument, which is the same as the context object at execution time, the other parameters of the calling method are passed to the borrowed method as arguments, and apply is two arguments, and the second parameter is passed as an array. So you can say
Call, apply method The difference is that, from the second parameter, called method parameters will be passed to the borrowed method as parameters, and apply directly put these parameters in an array and then passed, and finally the parameter list of the Borrowing method is the same.
On the usage and difference of application and call in JavaScript (RPM)