Creating new object using jsClass

Hi there!

I’m using Kotling 1.0.5,
and was wondering is it in some way, easy or hard, possible to create new object using jsClass (or some of it’s internals)? To make it simpler (if the way is hacky way), let’s say the class has a constructor with no arguments or at most one.

Thanks!

Something like this:

class C {
    override fun toString() = "C instance"
}

fun main(args: Array<String>) {
    val c = jsClass<C>().newInstance()
    println(c)
}

fun <T> JsClass<T>.newInstance(): T {
    inline fun callCtor(ctor: dynamic) = js("new ctor()")
    return callCtor(asDynamic()) as T
}

Also, I would recommend you to try upcoming 1.1-M03, which has class literal syntax. jsClass function will be deprecated soon.

Hi! Thanks for the help! I will try this out soon.

I guess I will stick with 1.0.5, as we are already in production with our Kotlin JS code.

Hi again!

Something related… is there a way, now or in the future to get the package name of a" jsClass"?

Currently, it’s impossible. As for future, it’s a matter of discussion. Why do you need this?

I’m creating a Javascript Web framework using Kotlin (JS).

Among other things the framework have many components, these are styled using css classes. To avoid CSS class name crashes I’m prefixing every component with it’s package name.

Example: The css class name of a component com.myapp.Button would then be ‘com-myapp-Button’.

So the motivation is then to avoid maintaining these manually. Having access to a class’ package name programatically would then make it possible.

Thanks or listening!

Jørund