Javascript ES6级结构
我想问一下JavaScript ES6中的类构造。 将类名放在从母类扩展而来的其他类的构造函数中可以吗?有点困惑Javascript ES6级结构,javascript,class,ecmascript-6,Javascript,Class,Ecmascript 6,我想问一下JavaScript ES6中的类构造。 将类名放在从母类扩展而来的其他类的构造函数中可以吗?有点困惑 class Brick { constructor(x,y,graphic,width,height,type,live, speed){ this.x = x this.y = y this.graphic = graphic this.width = width this.height = height
class Brick {
constructor(x,y,graphic,width,height,type,live, speed){
this.x = x
this.y = y
this.graphic = graphic
this.width = width
this.height = height
this.type = type
this.live = live
this.speed = speed
}
print(){
console.log(this.y)
console.log(this.x)
console.log(this.graphic)
console.log(this.width)
console.log(this.height)
console.log(this.type)
console.log(this.live)
}
init(){
console.log('added to board')
}
}
现在,我想让类wchih从Brick类扩展为:
class BrickRed extends Brick {
constructor(Brick){
super(...arguments)
this.graphic = "red.jpg"
this.live = 15
}
}
我不确定它是否合适,因为我找不到任何教程,如果它是上面介绍的。
正是这两行:constructorBrick和super…arguments
从我看到的教程中,最好也是唯一的选择是这样做:
class BrickBlue extends Brick {
constructor(x,y,graphic,width,height,type,live, speed){
super(x,y,graphic,width,height,type,live, speed)
this.graphic = "blue.jpg"
this.live = 10
}
}
但这看起来很难看,我想改进一下
将类名放在从母类扩展而来的其他类的构造函数中可以吗
没有。正确的方法是您的第二个代码片段。但是,如果BrickBlue硬编码某些道具,则无需在构造函数中传递它们:
class BrickBlue extends Brick {
constructor(x,y,width,height,type,speed){
super(x,y,"blue.jpg",width,height,type,10,speed)
}
}
如果你在找像这样的东西
class BrickBlue extends Brick {
constructor(args-of-Brick)
没有这样的事
但这看起来很难看,我想改进一下
是的,很长的参数列表很难看,而且由于JS还不支持命名参数,所以您也无能为力。但是,您可以考虑将相关参数分组为单独的对象:
class Brick {
constructor(position, graphic, size, type, behaviour)
位置是{x:10,y:20}等
另一个选项是为对象提供整个参数列表,从而模仿命名参数:
class Brick {
constructor({x, y, graphic, width, height, type, live, speed}) {
...
new Brick({
x: 1,
y: 2,
graphic: ...
...
})
在派生类中:
class BrickBlue extends Brick {
constructor(args) {
super({
...args,
graphic: 'blue.jpg',
live: 10
})
}
将类名放在从母类扩展而来的其他类的构造函数中可以吗
没有。正确的方法是您的第二个代码片段。但是,如果BrickBlue硬编码某些道具,则无需在构造函数中传递它们:
class BrickBlue extends Brick {
constructor(x,y,width,height,type,speed){
super(x,y,"blue.jpg",width,height,type,10,speed)
}
}
如果你在找像这样的东西
class BrickBlue extends Brick {
constructor(args-of-Brick)
没有这样的事
但这看起来很难看,我想改进一下
是的,很长的参数列表很难看,而且由于JS还不支持命名参数,所以您也无能为力。但是,您可以考虑将相关参数分组为单独的对象:
class Brick {
constructor(position, graphic, size, type, behaviour)
位置是{x:10,y:20}等
另一个选项是为对象提供整个参数列表,从而模仿命名参数:
class Brick {
constructor({x, y, graphic, width, height, type, live, speed}) {
...
new Brick({
x: 1,
y: 2,
graphic: ...
...
})
在派生类中:
class BrickBlue extends Brick {
constructor(args) {
super({
...args,
graphic: 'blue.jpg',
live: 10
})
}
构造器里克什么也不做。或者更准确地说,它只是说构造函数接受一个参数,然后调用该参数Brick。因为你从来没有使用过它,所以最终也没关系。或者更确切地说,是这样,但实际上只传递所有参数,因此忽略参数。你可以称它为foo或x,它也会有相同的。constructorBrick什么都不做。或者更准确地说,它只是说构造函数接受一个参数,然后调用该参数Brick。因为你从来没有使用过它,所以最终也没关系。或者更确切地说,是这样,但实际上只传递所有参数,因此忽略参数。你可以把它叫做foo或x,它也会是一样的。我要说的是,蓝瑞克不需要是个砖头的孩子。好吧,它可能会,如果是这样,将参数传递给父级是正确的,但似乎它是留给工厂来给您的,而不是有一个特定的构造函数。如果你想要另一个对象BouncyBrick呢?一块砖能有弹性和蓝色吗?如果你也想要秘砖呢?VLAZ:当然,在务实的程序员当中,继承通常是不赞成的,但是大多数书籍和课程仍然认为它是OOP的重要组成部分,所以我想我们暂时不得不处理它。我要说的是,蓝瑞克不需要是个砖头孩子。好吧,它可能会,如果是这样,将参数传递给父级是正确的,但似乎它是留给工厂来给您的,而不是有一个特定的构造函数。如果你想要另一个对象BouncyBrick呢?一块砖能有弹性和蓝色吗?如果你也想要秘砖呢?VLAZ:当然,在务实的程序员当中,继承通常是不赞成的,但是大多数书籍和课程仍然认为它是OOP的重要组成部分,所以我想我们暂时不得不处理它。