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