How can I call a function named apply() defined on a Javascript object from Scala.js code?
The obvious solution does not work since apply is always compiled into a function call on its parent object by Scala.JS (?):
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
applyunless you definitely want to overwrite the defaultapplybehaviour: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Object instanceof Functionif you're not sure. Of course, the reverse is also true. Expecting JavaScript to behave like a classical Object Oriented language will result in confusion sooner or later. stackoverflow.com/questions/7018023/…test = { apply: function() {} }; test()testis an anonymous object - which is indeed not a function - but also cannot be instantiated or called because you have declared it as a variable rather than an object. So you can calltest.apply()but test is the name of the variable not the name of a type.