Skip to content

Latest commit

 

History

History
72 lines (53 loc) · 1.57 KB

oo-4-invoking-object-methods.md

File metadata and controls

72 lines (53 loc) · 1.57 KB

Invoking object methods

Function vs. Methods

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!');
};

Calling Methods

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!'

A Method Can Access the Object it was Called On

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