Scala.js 引用使用这个

Scala.js referencing using this

我将 scala.js (v0.6.13) 与 Highcharts facade 一起使用,我 运行 遇到了障碍,试图访​​问我通常会在 [=27= 中访问的一些变量] 使用 'this' 和 'chart'。这是一个例子:

咖啡脚本:

tooltip: {
  enabled: true,
  positioner: (labelWidth, labelHeight, point) ->
    return { x: chart.plotWidth - labelWidth + chart.plotLeft, y: 17 }
  formatter: () ->
    x = this.x
    point = this.points.find (p) -> x == p.x
    ...

我的问题是如何在 scala.js 中的格式化程序和定位器函数中访问 "this.x" 和 "chart.plotWidth"?到目前为止,这是我的 Scala 代码:

override val tooltip: Cfg[Tooltip] = Tooltip(
  formatter = { () =>
    "what?"
  }: js.Function0[String],
  positioner = { (labelWidth: Any, labelHeight: Any, point: Object) =>
    js.Dynamic.literal(
      x = labelWidth,
      y = 17)
  }: js.Function3[Any, Any, Object, Object]
)

编辑:图表属于高图表。

您需要使用 js.ThisFunctionN 来显式捕获 JavaScript 的特殊 this 作为 Scala.js 中的普通参数。

positioner = { (thiz: js.Dynamic, labelWidth: Any, labelHeight: Any, point: Object) =>
  // here the variable `thiz` holds what would be `this` in JS
  ...
}: js.ThisFunction3[js.Dynamic, Any, Any, Object, Object]

将 Scala 匿名函数转换为 js.ThisFunction 时,this 参数作为第一个参数传入。

有关详细信息,请参阅 https://www.scala-js.org/doc/interoperability/types.html

对于 chart,您的问题没有提供足够的上下文来了解您的 CoffeeScript 代码中的 chart 是什么。但我想只要在 Scala.js 中使用 chart 就可以完成原始代码所做的一切。