[英]How to extend a class property inherited from parent class in ES6?
I have the follow code:我有以下代码:
class Parent {
some_class_property = [1,2,3];
some_other_methods(){...}
}
class Child extends Parent {
some_class_property = super.some_class_property.push(4);
}
The console gives me a syntax error, saying that the keyword super
is unexpected.控制台给我一个语法错误,说关键字super
是意外的。
If class properties are allowed in ES6, then what's the point not allowing it to be extended in child classes?如果 ES6 中允许 class 属性,那么不允许在子类中扩展它有什么意义呢? If this is not the right way to do it, then how?如果这不是正确的方法,那怎么办? Thanks.谢谢。
It looks like super
references are not permitted inside class fields, which is why your current code throws an error.看起来 class 字段中不允许super
引用,这就是您当前的代码引发错误的原因。
But, the some_class_property
is put onto the instantiated object itself in the superclass constructor (well, in the class field, which is effectively syntax sugar for putting it onto the object in the superclass constructor), which means you can reference it in the child class by referencing this.some_class_property
. But, the some_class_property
is put onto the instantiated object itself in the superclass constructor (well, in the class field, which is effectively syntax sugar for putting it onto the object in the superclass constructor), which means you can reference it in the child class通过引用this.some_class_property
。 You aren't referencing a shadowed method or property, so super
isn't needed:您没有引用阴影方法或属性,因此不需要super
:
class Parent { some_class_property = [1, 2, 3]; } class Child extends Parent { some_class_property = this.some_class_property.push(4) } const c = new Child(); console.log(c.some_class_property);
Also keep in mind that .push
returns the new length of the array, which is why the result of the above snippet is 4
.还要记住.push
返回数组的新长度,这就是上面代码片段的结果是4
的原因。 (If you wanted to make a copy of the some_class_property
array, for whatever reason, instead use some_class_property = [...this.some_class_property, 4]
) (如果您想复制some_class_property
数组,无论出于何种原因,请改用some_class_property = [...this.some_class_property, 4]
)
The time to use super
is when a property exists on the child instance, or the child prototype, but you want to reference a property on the parent prototype, eg:使用super
的时间是子实例或子原型上存在属性,但您想引用父原型上的属性时,例如:
class Parent { method() { console.log('parent method'); } } class Child extends Parent { method() { console.log('child method'); } invokeParentMethod() { super.method(); } } const c = new Child(); c.method(); c.invokeParentMethod();
Public and private properties are Javascript features at an experimental stage (ES11?).公共和私有属性是 Javascript 功能处于实验阶段(ES11?)。 For the past few years for ES6, people have been doing this:在 ES6 的过去几年里,人们一直在这样做:
class Parent {
constructor(x){
this.property1 = [1,2,3];
...
}
some_other_methods(){...}
}
Parent.property2 = [4,5,6]; // to access: Parent.property1
Parent.prototype.property3 = [7,8,9]; // to access: obj.property2
class Child extends Parent {
constructor(x){
super(x);
// ...
}
some_other_methods(){...}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.