【发布时间】:2016-03-06 15:38:35
【问题描述】:
我有这个 JavaScript 类/构造函数:
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
【问题讨论】:
标签: javascript scala scala.js