Redefine the setter, and the getter must also be redefined

class AbstractClass {

    constructor() {
    }

    set property(value) {
        this.property_ = value;
    }

    get property() {
        return this.property_;
    }

}

class Subclass extends AbstractClass {

    constructor() {
        super();
    }

    set property(value) {
        super.property = value;
        if (!(this.property_ instanceof SubclassAssociatedClass)) throw new TypeError();
    }

    //get property() {
    //  return super.property;
    //}

}

Override the setattribute method and it seems that the method getshould also be redefined, otherwise it returns undefined(i.e. the method is getnot inherited, uncomment the subclass method get property()above, and everything works fine).

I assume this is part of the specification. It will follow, although it is possible if the behavior was the result of cross-compilation. Just to be sure, is this the right way to code overridden setters and getters (both at the same time or not at all)?

+17
source share
1

, ( ). (.property ), , . , accessor getter, undefined.

, ES5.

+11

Source: https://habr.com/ru/post/1619549/


All Articles