Presented below is an example of a Scala object:
@JSExportTopLevel("Calculator")
object Calculator {
@JSExport
def calculate(): BigDecimal = 3.14
}
The exported singleton method can be invoked from a JavaScript application, but the result may not behave as expected:
let result = Calculator.calculate()
assert(result == 123); // passes
assert(result === 123); // fails
assert(result + 1 == 124); // fails, actually returns "1241"
Upon inspecting result
in the browser console, it appears as follows:
Object { s_math_BigDecimal__f_bigDecimal: {…}, s_math_BigDecimal__f_mc: {…}, s_math_BigDecimal__f_computedHashCode: 1565550863 }
s_math_BigDecimal__f_bigDecimal: Object { Ljava_math_BigDecimal__f__hashCode: 0, "Ljava_math_BigDecimal__f_java$math$BigDecimal$$_bitLength": 9, "Ljava_math_BigDecimal__f_java$math$BigDecimal$$_scale": 2, … }
s_math_BigDecimal__f_computedHashCode: 1565550863
s_math_BigDecimal__f_mc: Object { Ljava_math_MathContext__f_precision: 34, Ljava_math_MathContext__f_roundingMode: {…} }
<prototype>: Object { constructor: $c_s_math_BigDecimal(bigDecimal, mc), hashCode__I: hashCode__I(), equals__O__Z: equals__O__Z(that), … }
...
"$plus__s_math_BigDecimal__s_math_BigDecimal": function $plus__s_math_BigDecimal__s_math_BigDecimal(that)
byteValue__B: function byteValue__B()
...
toString__T: function toString__T()
<prototype>: Object { constructor: $c_s_math_ScalaNumber() }
Understanding how to effectively utilize this outcome can be challenging. Is there a need to export a different type or pursue a different approach?