This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
With the
method, you can write a method that can be used on different objects.const person = { firstName:"John", lastName: "Doe", fullName: function () { return this.firstName + " " + this.lastName; } } // This will return "John Doe": person.fullName();
In an object method, | refers to the object.
Alone, | refers to the global object.
In a function, | refers to the global object.
In a function, in strict mode, | is undefined.
In an event, | refers to the element that received the event.
Methods like | , , and can refer to any object.
const person = { fullName: function() { return this.firstName + " " + this.lastName; } } const person1 = { firstName:"John", lastName: "Doe" } const person2 = { firstName:"Mary", lastName: "Doe" } // This will return "John Doe": person.fullName.call(person1);
This example calls the fullName method of person, using it on person2:
const person = { fullName: function() { return this.firstName + " " + this.lastName; } } const person1 = { firstName:"John", lastName: "Doe" } const person2 = { firstName:"Mary", lastName: "Doe" } // This will return "Mary Doe" person.fullName.call(person2);
The
method can accept arguments:const person = { fullName: function(city, country) { return this.firstName + " " + this.lastName + "," + city + "," + country; } } const person1 = { firstName:"John", lastName: "Doe" } person.fullName.call(person1, "Oslo", "Norway");
Eventually the navigation links, above, will be replaced by these (previous) and (next) buttons below.
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon