When a function belongs to an object, we call it a method.
Inside an object method, we can use the this
keyword. Methods understand that this
is referring to the object itself.
Take a look at this example:
const person = {
firstName: 'Bob',
lastName: 'Marley',
getFullName: function () {
return this.firstName + ' ' + this.lastName;
},
};
const fullName = person.getFullName();
console.log(fullName);