We talked about the Javascript Call method before. This time we will talk about the apply method, which is similar to the Call method.
apply vs call
The difference between the two is: is it a parameter or an array of parameters that is passed
This is how to use call
theFunction.call(valueForThis, arg1, arg2, ...)
And this one is apply
theFunction.apply(valueForThis, arrayOfArgs)
Therefore
arrayOfArgs = [arg1, arg2, ...];
Javascript apply method
Let’s take a look at the previous usage of call
function print(p1, p2) {
console.log( p1 ' ' p2);
}
print.call(undefined, "Hello", "World");
From the above description, we can conclude that when
args = "Hello", "World";
function print(p1, p2) {
console.log( p1 ' ' p2);
}
print.call(undefined, args);
The two are equivalent, and in fact they are also equivalent, and the output result is also "Hello, World"!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn