例如,如果我有這樣的事情:
class X{
constructor({one, two}){
this.one = one
this.two = two
}
}
class Y extends X{
constructor(props, three){
super(props)
this.three = three
}
}
我想創建一個物件:
console.log(new Y({one: 1, two: 2, three: 3}))
是否可以?我嘗試了其他方法,例如:
console.log(new Y({one: 1, two: 2}, 3))
console.log(new Y({one: 1, two: 2}, {three: 3}))
但他們不舒服。
uj5u.com熱心網友回復:
你可以這樣做......
只需使用解構賦值
class X {
constructor({one, two}) {
this.one = one
this.two = two
}
displaying() {
console.log(this)
}
}
class Y extends X {
constructor({three, ...OneTwo}) {
super(OneTwo)
this.three = three
}
}
let zz = new Y({one: 1, two: 2, three: 3})
zz.displaying()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/398276.html
標籤:javascript 班级 遗产 子类
上一篇:每當您使用jquery滾動到每個li時,如何向它添加一個類
下一篇:從同一個父類的另一個類訪問物件
