We can extend functionality to objects by adding methods to them. A method is a property of an object whose value is a function.
const developer = {
name: 'Andrew'
};
developer.sayHello = function () {
console.log('Hi there!');
};
We can access a function in an object using the property name. Just like calling a function, an object's method is called by adding parentheses at the end of the method's name.
const developer = {
name: 'Andrew',
sayHello: function () {
console.log('Hi there!');
}
};
developer.sayHello();
// 'Hi there!'
developer['sayHello']();
// 'Hi there!'
Using this
, methods can directly access the object that it is called on.
const triangle = {
type: 'scalene',
identify: function () {
console.log(`This is a ${this.type} triangle.`);
}
};
triangle.identify();
// 'This is a scalene triangle.'
When you say this
, what you're really saying is "this object" or "the object at hand."
Using this
to change a object property:
const chameleon = {
color: 'green',
changeColor: function() {
this.color = 'pink'
}
};
chameleon.changecolor();
chameleon.color
//pink
Udacity course: Object-Oriented JavaScript ["this" in Method](https://javascript.info/object-methods#this-in-methods