A couple of question regarding `js("...")` code b...
# javascript
u
A couple of question regarding
js("...")
code block. In pre 1.4 I had this code to determine if I'm running on node or in browser (it's inside an actual implementation of expected method in multi platform project)
Copy code
val runningOnNode = js(
            "if (typeof window === 'undefined') {\n" +
                    "             true;\n" +
                    "    } else {\n" +
                    "            false;\n" +
                    "    }"
        )
Since switching to 1.4 and enabling IR, that block started returning
undefined
where it was previously a boolean true or false. I worked around this with this:
Copy code
val runningOnNode = js(
            "var isNode = false;\n" +
                    "if (typeof window === 'undefined') {\n" +
                    "             isNode = true;\n" +
                    "    } else {\n" +
                    "            isNode = false;\n" +
                    "    }\n" +
                    "return isNode;"
        )
So I have two questions, and please consider that my JS knowledge is very limited: 1. Is this expected change that came with 1.4 or is it a bug? 2. Am I using
js(...)
code block in a way that it was not meant to be used, and causing the issue myself?
t
Copy code
val isNode:Boolean = jsTypeOf(window) == "undefined"
u
That looks clean, thanks!
K 1
g
You also can use
"""some multiline code"""
to avoid all those \n and concatenations
I would also report this case to kotl.in/issue, even if it’s expected change, would be nice if it would be documented for js function