Hello. I tried to switch to IR compiler and found ...
# javascript
a
Hello. I tried to switch to IR compiler and found the following issue
Copy code
class IRTest {
    val numberToText = js("{'1' : 'one', '2' : 'two'}")

    @Test
    fun test() {
        val key = "1"
        assertEquals(true, js("key in this.numberToText"))
    }
}
fails with
Cannot use 'in' operator to search for '1' in undefined
while
Copy code
class IRTest {
    val numberToText = js("{'1' : 'one', '2' : 'two'}")

    @Test
    fun test() {
        val key = "1"
        val numberToTextCopy = this.numberToText
        assertEquals(true, js("key in numberToTextCopy"))
    }
}
works as expected. Could anyone explain this behavior? Thanks in advance for your help!
t
My guess would be that the name 'numberToTextCopy' is not 'numberToTextCopy' in the generated JS code. The 'js' function just puts a string into the generated code, it does not follow any optimisation the compiler might do (like minimising names).
k
This is actually a documented behavior, you are not allowed to use instance members in the js() function, see https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/js.html The fact that it works with the old JS compiler is an implementation detail. I personally would love if Kotlin stopped mangling names unnecessarily but it’s how the IR compiler rolls at the moment.
a
Thanks, this is very helpful