Analysis of the apply () method in Javascript, javascriptapply
We have mentioned the Javascript Call method before. This time we will talk about the apply method similar to the Call method.
Apply vs call
The difference between the two is: whether the parameter is passed or the parameter Array
This is the call usage.
Copy codeThe Code is as follows:
TheFunction. call (valueForThis, arg1, arg2 ,...)
This is apply.
Copy codeThe Code is as follows:
TheFunction. apply (valueForThis, arrayOfArgs)
Therefore
Copy codeThe Code is as follows:
ArrayOfArgs = [arg1, arg2,...];
Javascript apply Method
First, let's take a look at the usage of the previous call.
Copy codeThe Code is as follows:
Function print (p1, p2 ){
Console. log (p1 + ''+ p2 );
}
Print. call (undefined, "Hello", "World ");
From the above description, we can conclude that when
Copy codeThe Code is as follows:
Args = "Hello", "World ";
Function print (p1, p2 ){
Console. log (p1 + ''+ p2 );
}
Print. call (undefined, args );
The two are equivalent, but they are also equivalent. The output result is also "Hello, World "!