Next question, I'm using this function to create a...
# javascript
r
Next question, I'm using this function to create a JS object instance from JS constructor:
Copy code
inline fun <reified T> Any?.createInstance(vararg args: dynamic): T {
    val jsClass = this
    val argsArray = (listOf<dynamic>(null) + args).toTypedArray()
    return js("new (Function.prototype.bind.apply(jsClass, argsArray))").unsafeCast<T>()
}
like this:
Copy code
external interface Ext
val constructor: Any = require("...").something // returns a function which should be called with new operator in JS
val ext: Ext = constructor.createInstance(it)
It doesn't work with IR anymore. How can I replace this?
d
What is the problem with it?
Whats the generated JS output?
r
I think local variables can no longer be used in
js()
block
Copy code
var jsClass_1_4 = tmp0_createInstance_0_2;
      var tmp0_toTypedArray_0_3_6 = plus_1(listOf_1(null), tmp1_createInstance_0_3);
      var argsArray_2_5 = copyToArray_0(tmp0_toTypedArray_0_3_6);
      var tmp1_unsafeCast_0_4_7 = new (Function.prototype.bind.apply(jsClass, argsArray))();
d
Rename the
jsClass
variable to something else
That name causes mangling because the compiler thinks there is a collision with the top level
jsClass
property
r
And what about argsArray? It's mangled as well.
d
Hm. Right. Let me try something
Remove
inline
from the function and the mangling goes away
r
Thx, it fixed my problem