Is there a way to initialize JsClass<T>?
# javascript
s
Is there a way to initialize JsClass<T>?
d
T::class.js
Or what exactly do you mean by "initialize"?
s
Sorry, not very clear. Here is the context for the problem.
Copy code
class Example


fun doSomething(JsClass: dynamic) {

    val instance = JsClass() // isn't called with 'new' key word, would like it to be
}

fun main(){
    doSomething(Example::class.js)
}
I can change it from dynamic, but I'm not sure to what.
d
This type of reflection is not supported for the Javascript target yet.
You should probably accept a function type and use a reference to the constructor instead of a
JsClass
or `KClass`:
Copy code
fun <T : Any> doSomething(cstr: () -> T) {
    val instance = cstr()
}

doSomething(::Example)
s
I need to work with the constructor reference directly to override it's prototype's _init function then call the constructor.
Or at least that's what I'm trying to do, need is a strong word
d
Kotlin doesn't have a syntax for accessing the prototype directly, so you probably have to use some inline JS here
s
I've been able to do everything except call the constructor. I think I might try to write a 'new' keyword implementation as an extension function of the JsClass<T> but I had hoped there was some way to call the ref. Doesn't seem it though. I appreciate the confirmation.
d
Yeah, Kotlin does not have this concept of just calling a constructor like Javascript does.
val instance = js("new myConstructorRef()")
should work
s
Well there you go, haha. Didn't think of that. (not a fan of the inline js, feels like cheating), but it's get's me moving again. Thanks!
d
Well, what you are doing is not exactly kotlin-esque
Mucking around with an object's prototype is very specific to JS and not really something you would do in Kotlin