【问题标题】:Access JS this from Scala.js从 Scala.js 访问 JS
【发布时间】:2017-11-12 19:08:35
【问题描述】:

我正在尝试将此代码从 js 库文档转换为 scala.s:

$('#myTable').on( 'click', 'tbody td', function () {
    editor.inline( this, {
        submitOnBlur: true
    } );
} );

我试过的代码:

$("#table").on("click", ".editable", (thiz: js.Dynamic) => {
  editor.inline(thiz, JC(
    submitOnBlur = true
  ))
})

但它给了我错误:

无法读取 f.inline 处未定义的属性“内容”

【问题讨论】:

  • 你为 jQuery 使用什么外观库?即,您的构建/源代码中的哪些依赖项为您提供了$
  • @sjrd 我有"be.doeraene" % "scalajs-jquery_sjs0.6_2.12" % "0.9.2"

标签: javascript scala scala.js


【解决方案1】:

你写的回调函数,即

(thiz: js.Dynamic) => {
  editor.inline(thiz, JC(
    submitOnBlur = true
  ))
}

是一个有 1 个参数的函数(恰好称为 thiz),而不是一个接收 this 作为参数的函数。也就是说,相当于JS中的如下:

function(thiz) {
  editor.inline(thiz, JC(...))
}

要访问this,您需要强制回调函数为js.ThisFunction,如下所示:

((thiz: js.Dynamic) => {
  editor.inline(thiz, JC(
    submitOnBlur = true
  ))
}): js.ThisFunction

这将采用 Scala lambda 的第一个参数(在本例中是唯一一个)并将其附加到 JavaScript 的 this 值,这就是您想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多