🔸getter/setter

JSvalueobjectproperty ⟩ getter/setter

(⭐️ ES5)

an accessor property (not a data property) that has a getter and/or a setter method.

// defining "accessor properties"

// method 1:
let obj = {
    // data property
    _value: 0,
    // accessor property (pair of methods)
    get prop() { return this._value },
    set prop(value) { this._value = value },
};

// method 2:
Object.defineProperty(obj, 'prop', {
    get() { ... },         // getter
    set(value) { ... },    // setter
});

obj.prop;            // getting obj.prop
obj.prop = value;    // setting obj.prop

🈯 synonyms: "accessor property"

Last updated