JavaScript logoJavaScript/
JS-0041

Getter without a setter pair in objectsJS-0041

Minor severityMinor
Anti-pattern categoryAnti-pattern

It's a common mistake in JavaScript to create an object with just a setter for a property but never have a corresponding getter defined for it. Without a getter, you cannot read the property, so it ends up not being used.

Bad Practice

const o = {
    set a(value) {
        this.val = value;
    }
};
// or
const o = {d: 1};
Object.defineProperty(o, 'c', {
    set: function(value) {
        this.val = value;
    }
});
const o = {
    set a(value) {
        this.val = value;
    },
    get a() {
        return this.val;
    }
};

const o = {d: 1};
Object.defineProperty(o, 'c', {
    set: function(value) {
        this.val = value;
    },
    get: function() {
        return this.val;
    }
});