๐Ÿ”ธgetter/setter

JS โŸฉ value โŸฉ object โŸฉ property โŸฉ 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