How can I access the new javascript keyword from scala.js? -
i porting javascript library scalajs. js objects created new keyword on javascript side, in cases.
trait point extends js.object { def normalize(length: double): point = js.native }
this seems work methods, however, doesn't work constructors.
@jsname("paper.point") object pointnative extends js.object { def apply(props: rectprops): rectangle = js.native }
the above code not work. passes type checks , compiles, @ runtime returns undefined.
if modified pointnative good.
import js.dynamic.{ global => g, newinstance => jsnew } object pointnative { def apply(props: rectprops): rectangle = jsnew(g.paper.point)(props).asinstanceof[point] }
is there way use @jsname , js.native new keyword?
thanks!
since, in javascript api, paper.point
needs instantiated new
keyword, need define pointnative class:
@jsname("paper.point") class pointnative(props: pointprops) extends js.object { ... }
which allows instantiate with
new pointnative(props)
just have done in javascript.
if want able instantiate just
pointnative(props)
then need define additional, non-js.object
companion apply()
method:
object pointnative { def apply(props: pointprops): pointnative = new pointnative(props) }
note that, if need companion pointnative
js.object
(because want define static methods of paper.point
in it), can pimp apply()
on implicit class instead:
implicit class pointnativecompanionops(val self: pointnative.type) extends anyval { def apply(props: pointprops): pointnative = new pointnative(props) }
Comments
Post a Comment