We talked about the Javascript call method, and this time we're going to say the Apply method like say and called.
Apply vs Call
The difference between the two is whether a parameter is passed, or an array of arguments
This is the use of call.
Copy Code code as follows:
Thefunction.call (Valueforthis, arg1, arg2, ...)
And this is the Apply
Copy Code code as follows:
Thefunction.apply (Valueforthis, Arrayofargs)
So
Copy Code code as follows:
Arrayofargs = [Arg1, arg2, ...];
Javascript Apply Method
Let's take a look at the previous call usage.
Copy Code code as follows:
function Print (p1, p2) {
Console.log (p1 + ' + p2 ');
}
Print.call (Undefined, "Hello", "World");
By the above narrative, we can conclude that when
Copy Code code 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 is also "Hello,world"!