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 |1 Answer
Reset to default 37Yes, 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
版权声明:本文标题:javascript - Override a setter, and the getter must also be overridden - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738006901a2048232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
__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