Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

javascript - 有没有更直接的方法可以在Javascript中实现呢?(Is there a more direct way to achieve this in Javascript?)

I have an inheritance like this:

(我有这样的继承:)

class Parent {
  some_object_property = ["some_object_property_depending_on_initialization"];
  constructor() {...}
}

class Child extends Parent {
  some_object_property = [...super.some_object_property, 'child_property']
}

Is there a way to let the Child inherit and extend object property some_object_property from the Parent ?

(有没有办法让ChildParent继承和扩展对象属性some_object_property ?)

I know I can achieve the end result through getter and setter .

(我知道我可以通过gettersetter达到最终结果。)

Is there a more direct way?

(有没有更直接的方法?)

  ask by 北美38fule translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Did you mean this?

(你是这个意思吗)

 class Parent { constructor() { this.someProp = ['someprop test', 'something else']; } } class Child extends Parent { constructor() { super(); this.someProp = [...this.someProp ,'added child prop']; } } let myChild = new Child(); console.log(myChild.someProp); 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...