In Scala.js 1.7.1, default parameter values specified in @js.native def facades are ignored during compilation. Instead, the parameter is omitted in the generated JavaScript call, allowing the underlying JavaScript implementation to handle its own default behavior (typically by checking if the parameter is undefined).
Key behaviors:
- The value provided in the Scala facade (e.g.,
y: Int = 2) has no impact on the actual JavaScript call. The parameter is simply omitted. - You can now use
= js.native as a default value in @js.native defs, which was previously unsupported for top-level method facades.
Example:
If you have a JavaScript object:
const Foo = {
bar(x, y) {
return x + (y === undefined ? 1 : y);
}
};
Defining the Scala facade as:
@js.native @JSGlobal
object Foo extends js.Object {
def bar(x: Int, y: Int = 2): Int = js.native
}
Calling Foo.bar(5) will compile to Foo.bar(5) in JavaScript. The JavaScript engine will then use its own default (1), ignoring the Scala-defined default of 2.
@js.native @JSGlobal
object Foo extends js.Object {
def bar(x: Int, y: Int = js.native): Int = js.native
}
// Calling this:
println(Foo.bar(5))
// Compiles to JS:
// Foo.bar(5)