admin管理员组

文章数量:1178543

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 set method of an attribute and it appears the get method must be overridden also, otherwise undefined is returned (i.e., the get method is not inherited, uncomment the subclass get property() method above and everything works fine).

I assume this is a part of the spec., it would follow though possibly if the behaviour was a consequence of cross compiling. Just to be sure, is this the correct way to code overridden setters and getters (both at the same time or not at all)?

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 set method of an attribute and it appears the get method must be overridden also, otherwise undefined is returned (i.e., the get method is not inherited, uncomment the subclass get property() method above and everything works fine).

I assume this is a part of the spec., it would follow though possibly if the behaviour was a consequence of cross compiling. Just to be sure, is this the correct way to code overridden setters and getters (both at the same time or not at all)?

Share Improve this question edited Dec 14, 2015 at 18:39 Bergi 664k159 gold badges1k silver badges1.5k bronze badges asked Mar 9, 2015 at 20:01 user5321531user5321531 3,2555 gold badges24 silver badges28 bronze badges 3
  • Can you edit your example so that the setter/getter is not just calling super? – Bergi Commented Mar 9, 2015 at 20:14
  • 1 This could be helpful: stackoverflow.com/questions/27400010/…. It discusses the problem in terms of __defineGetter__ etc., but the principle is the same: if you are defining/redefining the setter on a property that already has a getter, you need to retrieve the getter and reset it along with the setter. – user663031 Commented Mar 10, 2015 at 5:00
  • Just found this out the hard way, today. I was wondering why several of my properties became write-only ;-) – Kristian Oye Commented Dec 30, 2023 at 18:38
Add a comment  | 

1 Answer 1

Reset to default 37

Yes, this is intentional (a part of the spec). If an object has an own property (.property in your example), this property will be used and not an inherited one. If that property is existent, but is an accessor property without a getter, then undefined will be returned.

Notice that this behaviour has not changed from ES5.

本文标签: javascriptOverride a setterand the getter must also be overriddenStack Overflow