Classes & Objects¶
Classes¶
Kotlin classes transpile to ES6 classes. Properties in the primary constructor are automatically assigned in the JS constructor.
Transpiles to:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
println("Hello, " + name);
}
}
Inheritance¶
Inheritance is supported via ES6 extends and super().
Transpiles to:
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {
constructor(name) {
super(name);
}
}
Interfaces¶
Interfaces are not emitted at runtime because JavaScript does not have a native runtime representation for them. They are only used for compile-time checks in Kotlin.
Transpiles to:
Enums¶
Enums are transpiled to frozen JavaScript objects.
Transpiles to:
Objects¶
Singletons declared with object are transpiled to frozen JavaScript objects.
Transpiles to: