如何在 JavaScript 个子类中使用对象作为参数?
How to use an object as an argument in JavaScript subclasses?
例如,如果我有这样的东西:
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}))
但是他们不舒服。
你可以做到...
simply use Destructuring assignment
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()
例如,如果我有这样的东西:
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}))
但是他们不舒服。
你可以做到...
simply use Destructuring assignment
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()