apply() method, you can write a method that can be used on different objects.
apply() method is similar to the
call() method (previous chapter).
In this example the fullName method of person is applied on person1:
The difference is:
call() method takes arguments separately.
apply() method takes arguments as an array.
The apply() method is very handy if you want to use an array instead of an argument list.
apply() method accepts arguments in an array:
Compared with the
You can find the largest number (in a list of numbers) using the
Math.max() method instead.
The first argument (null) does not matter. It is not used in this example.
These examples will give the same result:
apply() method is not an object, it becomes the owner (object) of the invoked function. In "non-strict" mode, it becomes the global object.