We have talked about the Javascript Call method before, and this time we will talk about the apply method similar to the Call method.
apply vs call
The difference between the two is: is it passed or is it an array of parameters.
This is how to use call
The code copy is as follows:
theFunction.call(valueForThis, arg1, arg2, ...)
And this is apply
The code copy is as follows:
theFunction.apply(valueForThis, arrayOfArgs)
Therefore
The code copy is as follows:
arrayOfArgs = [arg1, arg2, ...];
Javascript apply method
Let's take a look at the usage of previous calls
The code copy is as follows:
function print(p1, p2) {
console.log(p1 + ' ' + p2);
}
print.call(undefined, "Hello", "World");
From the above description, we can conclude that
The code copy is as follows:
args = "Hello", "World";
function print(p1, p2) {
console.log(p1 + ' ' + p2);
}
print.call(undefined, args);
The two are equivalent, but in fact they are also equivalent, and the output is also "Hello, World"!