I am moving the JavaScript library to Scalajs. JS objects are created with a new keyword on the JavaScript side, so this is what I do in most cases.
trait Point extends js.Object {
def normalize(length: Double): Point = js.native
}
This seems to work well for methods, however it does not work for constructors.
@JSName("paper.Point")
object PointNative extends js.Object {
def apply(props: RectProps): Rectangle = js.native
}
The code above does not work. It passes type checks and compilation, but at runtime it returns undefined.
If I changed PointNative, then everything is fine.
import js.Dynamic.{ global => g, newInstance => jsnew }
object PointNative {
def apply(props: RectProps): Rectangle = jsnew(g.paper.Point)(props).asInstanceOf[Point]
}
Is there a way to use @JSName and js.native with a new keyword?
Thank!
source
share