ScalaJS 中 require 之后的 new 等价物是什么?

What is the equivalent for new after require in ScalaJS?

我明白了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'); 应该是什么?

目前有点难看。要创建动态 class 的实例,您必须使用 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