在扩展本机 class 的 ScalaJS class 中调用重载的超级构造函数
Calling overloaded super constructor in ScalaJS class that extends a native class
我有这个 JavaScript class/constructor:
function Grid(size, tileFactory, previousState, over, won) {
this.size = size;
this.tileFactory = tileFactory;
this.cells = previousState ? this.fromState(previousState) : this.empty();
this.over = over ? over : false;
this.won = won ? won : false;
}
我使用这个 ScalaJS facade 映射的:
@js.native
class Grid[T <: Tile](val size: Int,
val tileFactory: TileFactory[T],
previousState: js.Array[js.Array[TileSerialized]],
val over: Boolean,
val won: Boolean) extends js.Object {
val cells: js.Array[js.Array[T]] = js.native
def this(size: Int, tileFactory: TileFactory[T]) = this(???, ???, ???, ???, ???)
...
}
我想扩展 Grid
class,我已经这样做了:
@ScalaJSDefined
class ExtendedGrid(
override val size: Int,
override val tileFactory: TileFactory[Tile],
previousState: js.Array[js.Array[TileSerialized]],
override val over: Boolean,
override val won: Boolean) extends Grid(size, tileFactory, previousState, over, won) {
...
}
但是现在我还需要为此ExtendedGrid
class实现重载的构造函数。
问题是,我该怎么做?
理想情况下,我想做类似的事情:
def this(size: Int, tileFactory: TileFactory[Tile]) = super(size: Int, tileFactory: TileFactory[Tile])
但据我了解,这在 Scala 中是不可能的。
为了尝试一下,我尝试简单地复制我在外观中定义的原始重载构造函数:
def this(size: Int, tileFactory: TileFactory[T]) = this(???, ???, ???, ???, ???)
确实编译但显然导致浏览器错误:
Uncaught scala.NotImplementedError: an implementation is missing
然后我尝试了:
def this(size: Int, tileFactory: TileFactory[Tile]) = this(size, tileFactory, this.empty(), false, false)
模仿原始 JavaScript 函数的行为,但无济于事。它会产生此错误:
this can be used only in a class, object, or template
您尝试调用的构造函数没有真正重载。它更接近 默认参数 和可选值。在JS中,默认参数基本都是undefined
。因此,您可以对父构造函数进行不同的建模:
@js.native
class Grid[T <: Tile](val size: Int,
val tileFactory: TileFactory[T],
previousState: js.UndefOr[js.Array[js.Array[TileSerialized]]] = js.undefined,
_over: js.UndefOr[Boolean] = js.undefined,
_won: js.UndefOr[Boolean] = js.undefined) extends js.Object {
val over: Boolean = js.native
val won: Boolean = js.native
val cells: js.Array[js.Array[T]] = js.native
...
}
然后您可以在定义 class 时模仿相同的结构:
@ScalaJSDefined
class ExtendedGrid(size: Int,
tileFactory: TileFactory[Tile],
previousState: js.UndefOr[js.Array[js.Array[TileSerialized]]] = js.undefined,
_over: js.UndefOr[Boolean] = js.undefined,
_won: js.UndefOr[Boolean] = js.undefined) extends Grid(size, tileFactory, previousState, _over, _won) {
...
}
顺便说一句,不要使用 override val
,因为您正在将值传递给父构造函数,并且您从 superclass.[=15= 获得了 val
]
我有这个 JavaScript class/constructor:
function Grid(size, tileFactory, previousState, over, won) {
this.size = size;
this.tileFactory = tileFactory;
this.cells = previousState ? this.fromState(previousState) : this.empty();
this.over = over ? over : false;
this.won = won ? won : false;
}
我使用这个 ScalaJS facade 映射的:
@js.native
class Grid[T <: Tile](val size: Int,
val tileFactory: TileFactory[T],
previousState: js.Array[js.Array[TileSerialized]],
val over: Boolean,
val won: Boolean) extends js.Object {
val cells: js.Array[js.Array[T]] = js.native
def this(size: Int, tileFactory: TileFactory[T]) = this(???, ???, ???, ???, ???)
...
}
我想扩展 Grid
class,我已经这样做了:
@ScalaJSDefined
class ExtendedGrid(
override val size: Int,
override val tileFactory: TileFactory[Tile],
previousState: js.Array[js.Array[TileSerialized]],
override val over: Boolean,
override val won: Boolean) extends Grid(size, tileFactory, previousState, over, won) {
...
}
但是现在我还需要为此ExtendedGrid
class实现重载的构造函数。
问题是,我该怎么做?
理想情况下,我想做类似的事情:
def this(size: Int, tileFactory: TileFactory[Tile]) = super(size: Int, tileFactory: TileFactory[Tile])
但据我了解,这在 Scala 中是不可能的。
为了尝试一下,我尝试简单地复制我在外观中定义的原始重载构造函数:
def this(size: Int, tileFactory: TileFactory[T]) = this(???, ???, ???, ???, ???)
确实编译但显然导致浏览器错误:
Uncaught scala.NotImplementedError: an implementation is missing
然后我尝试了:
def this(size: Int, tileFactory: TileFactory[Tile]) = this(size, tileFactory, this.empty(), false, false)
模仿原始 JavaScript 函数的行为,但无济于事。它会产生此错误:
this can be used only in a class, object, or template
您尝试调用的构造函数没有真正重载。它更接近 默认参数 和可选值。在JS中,默认参数基本都是undefined
。因此,您可以对父构造函数进行不同的建模:
@js.native
class Grid[T <: Tile](val size: Int,
val tileFactory: TileFactory[T],
previousState: js.UndefOr[js.Array[js.Array[TileSerialized]]] = js.undefined,
_over: js.UndefOr[Boolean] = js.undefined,
_won: js.UndefOr[Boolean] = js.undefined) extends js.Object {
val over: Boolean = js.native
val won: Boolean = js.native
val cells: js.Array[js.Array[T]] = js.native
...
}
然后您可以在定义 class 时模仿相同的结构:
@ScalaJSDefined
class ExtendedGrid(size: Int,
tileFactory: TileFactory[Tile],
previousState: js.UndefOr[js.Array[js.Array[TileSerialized]]] = js.undefined,
_over: js.UndefOr[Boolean] = js.undefined,
_won: js.UndefOr[Boolean] = js.undefined) extends Grid(size, tileFactory, previousState, _over, _won) {
...
}
顺便说一句,不要使用 override val
,因为您正在将值传递给父构造函数,并且您从 superclass.[=15= 获得了 val
]