JavaScript Reflection

At this time, JavaScript does not support the full Kotlin reflection API. The only supported part of the API is the ::class syntax which allows you to refer to the class of an instance, or the class corresponding to the given type. The value of a ::class expression is a stripped-down KClass implementation that only supports the simpleName and isInstance members.

In addition to that, you can use KClass.js to access the JsClass instance corresponding to the class. The JsClass instance itself is a reference to the constructor function. This can be used to interoperate with JS functions that expect a reference to a constructor.

Examples:

  1. class A
  2. class B
  3. class C
  4. inline fun <reified T> foo() {
  5. println(T::class.simpleName)
  6. }
  7. val a = A()
  8. println(a::class.simpleName) // Obtains class for an instance; prints "A"
  9. println(B::class.simpleName) // Obtains class for a type; prints "B"
  10. println(B::class.js.name) // prints "B"
  11. foo<C>() // prints "C"