3
我有这个JavaScript类/构造函数:调用重载在ScalaJS级的超级构造函数延伸的原生类
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门面映射:
@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
类,我已经这样做了:
@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
类重载构造函数的重载。
问题是,我该怎么做?
理想情况下,我想这样做:
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