Home > Article > Web Front-end > The difference between Function.prototype.apply and Function.prototype.call in JavaScript
Function.prototype.apply and Function.prototype.call are methods that allow you to call a function with a specific this value and parameters. The main difference between the two is that apply allows you to pass in an array of parameters, while call requires you to list the parameters one by one.
Function .prototype.apply is a method that allows you to call a function with a specific this value and argument array.
The syntax for using apply is -
func.apply(thisArg, argsArray)
where thisArg is the value that will be used as this within the function. argsArray is the array of arguments that will be passed to the function.
The following is an example of calling a function using apply -
<!doctype html> <html> <head> <title>Examples</title> </head> <body> <div id="result"></div> <script> function sayHello(name) { return "Hello, " + name + "!"; } document.getElementById("result").innerHTML = sayHello.apply(null, ["John"]) </script> </body> </html>
The above code will print the following output.
Hello, John!
As you can see, we passed null for thisArg because we don't want to set this value. We passed an array for argsArray containing the parameter "John". The result is a call to the sayHello function with "John" as the name parameter.
Function.prototype.call is a function that allows you to call a method with a specific this value and parameter list.
The syntax for using call is
func.call(thisArg, arg1, arg2, ...)
where thisArg is the value that will be used as this within the function. arg1, arg2, ... are the parameters that will be passed to the function.
Here is an example using call Calling function -
<!doctype html> <html> <head> <title>Examples</title> </head> <body> <div id="result"></div> <script> function sayHello(name) { return "Hello, " + name + "!"; } document.getElementById("result").innerHTML = sayHello.call(null, ["John"]) </script> </body> </html>
The above code will print the following output .
Hello, John!
As you can see, we passed null for thisArg because we don't want to set this value. We take "John" as the only parameter. The result is a call to the sayHello function with "John" as the name parameter.
The following table highlights the main differences between Function.prototype.apply and Function.prototype.call-
Basics | Function.prototype.apply | Function.prototype.call | |
---|---|---|---|
Definition | This method allows us to call a function with a specific this value and argument array. | This method allows us to call a function with a specific this value and argument list. | |
Parameters | We pass an array of parameters. | We pass a parameter list. | |
Speed | Because it doesn't create a new function, it's faster than calling. | em> | Because each call creates a new function, it is slower than apply. |
Usage |
|
|
and The difference between call> method. The main difference between the two is how they accept arguments. These methods have different uses. You can view the usage rows in the table above.
The above is the detailed content of The difference between Function.prototype.apply and Function.prototype.call in JavaScript. For more information, please follow other related articles on the PHP Chinese website!