This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here!
Smartphone icons created by Freepik - Flaticon
11.5 JS Function Apply
Method Reuse
With the apply() method, you can write a method that can be used on different objects.
The JavaScript apply() Method
The apply() method is similar to the call() method (previous chapter).
In this example the fullName method of person is applied on person1 :
var person = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
var person1 = {
firstName: "Mary",
lastName: "Doe"
}
person.fullName.apply(person1); // Will return "Mary Doe"
The Difference Between call() and apply()
The difference is:
The call() method takes arguments separately .
The 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.
The apply() Method with Arguments
The apply() method accepts arguments in an array:
var person = {
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city + "," + country;
}
}
var person1 = {
firstName:"John",
lastName: "Doe"
}
person.fullName.apply(person1, ["Oslo", "Norway"]);
Compared with the call() method:
var person = {
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city + "," + country;
}
}
var person1 = {
firstName:"John",
lastName: "Doe"
}
person.fullName.call(person1, "Oslo", "Norway");
Simulate a Max Method on Arrays
You can find the largest number (in a list of numbers) using the Math.max() method:
Math.max(1,2,3); // Will return 3
Since JavaScript arrays do not have a max() method, you can apply the Math.max() method instead.
Math.max.apply(null, [1,2,3]); // Will also return 3
The first argument (null) does not matter. It is not used in this example.
These examples will give the same result:
Math.max.apply(Math, [1,2,3]); // Will also return 3
Math.max.apply(" ", [1,2,3]); // Will also return 3
Math.max.apply(0, [1,2,3]); // Will also return 3
JavaScript Strict Mode
In JavaScript strict mode, if the first argument of the apply() method is not an object, it becomes the owner (object) of the invoked function.
In "non-strict" mode, it becomes the global object.
JavaScript icons used in the buttons provided by ICONS8.COM . Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Functions - Function Declaration
JS Functions - Function Expressions part 1
JS Functions - Function Expressions part 2
JS Functions - The Function() Constructor part 1
JS Functions - The Function() Constructor part 2
JS Functions - Self-Invoking Functions
JS Functions - Functions Can Be Used as Values part 1
JS Functions - Functions Can Be Used as Values part 2
JS Functions are Objects part 1
JS Functions are Objects part 2
JS Arrow Functions part 1
JS Arrow Functions part 2
JS Default Parameters
JS Functions - Default Parameter Values
JS functions - The Rest Parameter
JS Functions - Finding the largest number
JS Functions - Sum of all arguments
JS Functions - Invoking a Function as a Function part 1
JS Functions - Invoking a Function as a Function part 2
JS Functions - The Global Object
JS Functions - Invoking a Function as a Method part 1
JS Functions - Invoking a Function as a Method part 2
JS Functions - Invoking a Function with a Function Constructor
JS Functions - All Functions are Methods
JS Functions - The JavaScript call() Method part 1
JS Functions - The JavaScript call() Method part 2
JS Functions - The JavaScript call() Method with Arguments
JS Functions - The JavaScript apply() Method
JS Functions - The apply() Method with Arguments
JS Functions - The call() Method with Arguments
JS Math.max() part 1
JS Math.max() part 2
JS Math.max() part 3
JS Math.max() part 4
JS Math.max() part 5
JS Function bind() part 1
JS Function bind() part 2
JS Function bind() part 3
JS Function bind() part 4
JS Functions - Global Variables part 1
JS Functions - Global Variables part 2
JS Functions - Global Variables part 3
JS Function Closures - Counter Dilemma part 1
JS Function Closures - Counter Dilemma part 2
JS Function Closures - Counter Dilemma part 3
JS Function Closures - Nested Functions
JS Closures