我该如何从Scala.js代码中调用一个名为
显然的解决方案不起作用,因为Scala.js总是将
apply()
的Javascript对象上定义的函数?显然的解决方案不起作用,因为Scala.js总是将
apply
编译成其父对象上的函数调用。var test = {
apply: function(idx) {...},
apple: function(idx) {...}
}
trait Test extends js.Object {
def apple(idx: Int) : Int = ???
def apply(idx: Int) : Int = ???
}
val test = js.Dynamic.global.test.asInstanceOf[Test]
test.apple(1) // works, of course
test.apply(1) // does not work (seems to be compiled into the call test(1) ?)
js.Dynamic.global.test.apply(1) // does not work either
apply
,除非您确实想要覆盖默认的apply
行为:http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply - glenatrontest = { apply: function() {} }; test()
。 - jokadetest
是一个匿名对象 - 它确实不是一个函数 - 但也不能被实例化或调用,因为你将它声明为一个变量而不是一个对象。所以你可以调用test.apply()
,但 test 是变量的名称,而不是类型的名称。 - glenatron