snippet

Getters with this into object javascript

Here is how to use the this operator in a javascript object. It allows to return contextualized values according to the values of the object. This technique is useful to avoid creating external functions to the object, while being agnostic to the values of the object.

const person = {
  first: 'Jane',
  last: 'Doe',
  get fullName() {
    return `${this.first} ${this.last}`;
  },
};

person.full // Return Jane Doe string
Related snippets