我正在将一个JavaScript库移植到Scalajs.JS对象是在JavaScript端使用new关键字创建的,所以这就是我在大多数情况下所做的.
trait Point extends js.Object {
def normalize(length: Double): Point = js.native
}
Run Code Online (Sandbox Code Playgroud)
这似乎适用于方法,但是,这对构造函数不起作用.
@JSName("paper.Point")
object PointNative extends js.Object {
def apply(props: RectProps): Rectangle = js.native
}
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用.它传递类型检查和编译,但在运行时它返回undefined.
如果我像这样修改了PointNative那么一切都很好.
import js.Dynamic.{ global => g, newInstance => jsnew }
object PointNative {
def apply(props: RectProps): Rectangle = jsnew(g.paper.Point)(props).asInstanceOf[Point]
}
Run Code Online (Sandbox Code Playgroud)
有没有办法将@JSName和js.native与new关键字一起使用?
谢谢!