Property getters and setters

Getters and setters

Accessor properties are represented by “getter” and “setter” methods. In an object literal they are denoted by get and set:

  • get – a function without arguments, that works when a property is read,
  • set – a function with one argument, that is called when the property is set,





                     
                let obj = {
  get propName() {
    // getter, the code executed on getting obj.propName
  },

  set propName(value) {
    // setter, the code executed on setting obj.propName = value
  }
};

Comments

Popular Posts