如何在名称中使用`.`定义全局范围方法
how to define global scope method with `.` in name
我正在尝试为以下对象定义立面:https://facebook.github.io/jest/docs/en/api.html#testonlyname-fn
@JSGlobalScope
@js.native
object JestGlobal extends js.Object {
def test(str: String, function: js.Function0[_]): Unit = js.native
@JSName("test.only")
def testOnly(str: String, function: js.Function0[_]): Unit = js.native
def expect[T](in:T) : Matcher[T] = js.native
}
@js.native
trait Matcher[T] extends js.Object {
def toBe(in:T):Unit = js.native
}
Calling a method of the global scope whose name is not a valid
JavaScript identifier is not allowed. [error] See
https://www.scala-js.org/doc/interoperability/global-scope.html for
further information.
编辑:(答案)
def test : JestTestObject = js.native
@js.native
trait JestTestObject extends js.Object {
def only(str: String, function: js.Function0[_]): Unit = js.native
}
出于所有实际目的,没有名为 test.only
的 JS 函数这样的东西。更有可能存在名称为 test
的顶级对象,并且它有一个名为 only
的方法。您可以将其建模为:
@js.native
@JSGlobal("test")
object JestTest extends js.Object {
def only(str: String, function: js.Function0[_]): Unit = js.native
}
您还可以使用相同的对象来表示名称为 test
的顶级函数(因为显然库是这样显示的),方法是添加 apply
方法:
@js.native
@JSGlobal("test")
object JestTest extends js.Object {
// This is the test(...) function
def apply(str: String, function: js.Function0[_]): Unit = js.native
// This is the test.only(...) function
def only(str: String, function: js.Function0[_]): Unit = js.native
}
您作为自我回答的变体也是有效的,但可以使其更加地道,如下所示:
@js.native
@JSGlobalScope
object JestGlobal extends js.Object {
@js.native
object test extends js.Object {
// This is the test(...) function
def apply(str: String, function: js.Function0[_]): Unit = js.native
// This is the test.only(...) function
def only(str: String, function: js.Function0[_]): Unit = js.native
}
def expect[T](in: T): Matcher[T] = js.native
}
我正在尝试为以下对象定义立面:https://facebook.github.io/jest/docs/en/api.html#testonlyname-fn
@JSGlobalScope
@js.native
object JestGlobal extends js.Object {
def test(str: String, function: js.Function0[_]): Unit = js.native
@JSName("test.only")
def testOnly(str: String, function: js.Function0[_]): Unit = js.native
def expect[T](in:T) : Matcher[T] = js.native
}
@js.native
trait Matcher[T] extends js.Object {
def toBe(in:T):Unit = js.native
}
Calling a method of the global scope whose name is not a valid JavaScript identifier is not allowed. [error] See https://www.scala-js.org/doc/interoperability/global-scope.html for further information.
编辑:(答案)
def test : JestTestObject = js.native
@js.native
trait JestTestObject extends js.Object {
def only(str: String, function: js.Function0[_]): Unit = js.native
}
出于所有实际目的,没有名为 test.only
的 JS 函数这样的东西。更有可能存在名称为 test
的顶级对象,并且它有一个名为 only
的方法。您可以将其建模为:
@js.native
@JSGlobal("test")
object JestTest extends js.Object {
def only(str: String, function: js.Function0[_]): Unit = js.native
}
您还可以使用相同的对象来表示名称为 test
的顶级函数(因为显然库是这样显示的),方法是添加 apply
方法:
@js.native
@JSGlobal("test")
object JestTest extends js.Object {
// This is the test(...) function
def apply(str: String, function: js.Function0[_]): Unit = js.native
// This is the test.only(...) function
def only(str: String, function: js.Function0[_]): Unit = js.native
}
您作为自我回答的变体也是有效的,但可以使其更加地道,如下所示:
@js.native
@JSGlobalScope
object JestGlobal extends js.Object {
@js.native
object test extends js.Object {
// This is the test(...) function
def apply(str: String, function: js.Function0[_]): Unit = js.native
// This is the test.only(...) function
def only(str: String, function: js.Function0[_]): Unit = js.native
}
def expect[T](in: T): Matcher[T] = js.native
}