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:
class A
class B
class C
inline fun <reified T> foo() {
println(T::class.simpleName)
}
val a = A()
println(a::class.simpleName) // Obtains class for an instance; prints "A"
println(B::class.simpleName) // Obtains class for a type; prints "B"
println(B::class.js.name) // prints "B"
foo<C>() // prints "C"
当前内容版权归 kotlinlang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 kotlinlang .