ScalaJS中的新要求之后的等价物是什么?

时间:2016-02-19 09:29:26

标签: scala.js

我看到enter image description here

的javascript:

var D3Funnel = require('d3-funnel');
var chart = new D3Funnel('#funnel');

我设法通过使用:

让第一行在ScalaJS中工作

ScalaJS:

val Funnel = js.Dynamic.global.require("d3-funnel")

但是什么应该等同于ScalaJS中的第二行var chart = new D3Funnel('#funnel');

1 个答案:

答案 0 :(得分:2)

目前有点难看。要创建动态类的实例,您必须使用js.Dynamic.newInstance

import scala.scalajs.js

val chart = js.Dynamic.newInstance(Funnel)("#funnel")

从那里开始,我建议将chart转换为静态定义的API:

@js.native
trait Funnel extends js.Object {
  def someMethodOfFunnel(): Unit = js.native
}

val chart = js.Dynamic.newInstance(Funnel)("#funnel").asInstanceOf[Funnel]
chart.someMethodOfFunnel() // statically typed