Use Kotlin code from JavaScript
Depending on the selected JavaScript Module system, the Kotlin/JS compiler generates different output. But in general, the Kotlin compiler generates normal JavaScript classes, functions and properties, which you can freely use from JavaScript code. There are some subtle things you should remember, though.
If you have explicitly set your module kind to be plain
, Kotlin creates an object that contains all Kotlin declarations from the current module. This is done to prevent spoiling the global object. This means that for a module myModule
, all declarations are available to JavaScript via the myModule
object. For example:
fun foo() = "Hello"
Can be called from JavaScript like this:
alert(myModule.foo());
This is not applicable when you compile your Kotlin module to JavaScript modules like UMD (which is the default setting for both browser
and nodejs
targets), CommonJS or AMD. In this case, your declarations will be exposed in the format specified by your chosen JavaScript module system. When using UMD or CommonJS, for example, your call site could look like this:
alert(require('myModule').foo());
Check the article on JavaScript Modules for more information on the topic of JavaScript module systems.
Kotlin exposes its package structure to JavaScript, so unless you define your declarations in the root package, you have to use fully qualified names in JavaScript. For example:
package my.qualified.packagename
fun foo() = "Hello"
When using UMD or CommonJS, for example, your callsite could look like this:
alert(require('myModule').my.qualified.packagename.foo())
Or, in the case of using plain
as a module system setting:
alert(myModule.my.qualified.packagename.foo());
In some cases (for example, to support overloads), the Kotlin compiler mangles the names of generated functions and attributes in JavaScript code. To control the generated names, you can use the @JsName
annotation:
// Module 'kjs'
class Person(val name: String) {
fun hello() {
println("Hello $name!")
}
@JsName("helloWithGreeting")
fun hello(greeting: String) {
println("$greeting $name!")
}
}
Now you can use this class from JavaScript in the following way:
// If necessary, import 'kjs' according to chosen module system
var person = new kjs.Person("Dmitry"); // refers to module 'kjs'
person.hello(); // prints "Hello Dmitry!"
person.helloWithGreeting("Servus"); // prints "Servus Dmitry!"
If we didn't specify the @JsName
annotation, the name of the corresponding function would contain a suffix calculated from the function signature, for example hello_61zpoe$
.
Note that there are some cases in which the Kotlin compiler does not apply mangling:
external
declarations are not mangled.Any overridden functions in non-
external
classes inheriting fromexternal
classes are not mangled.
The parameter of @JsName
is required to be a constant string literal which is a valid identifier. The compiler will report an error on any attempt to pass non-identifier string to @JsName
. The following example produces a compile-time error:
@JsName("new C()") // error here
external fun newC()
warning
This feature is Experimental. Its design may change in future versions.
By applying the @JsExport
annotation to a top-level declaration (like a class or function), you make the Kotlin declaration available from JavaScript. The annotation exports all nested declarations with the name given in Kotlin. It can also be applied on file-level using @file:JsExport
.
To resolve ambiguities in exports (like overloads for functions with the same name), you can use the @JsExport
annotation together with @JsName
to specify the names for the generated and exported functions.
In the current IR compiler backend, the @JsExport
annotation is the only way to make your functions visible from Kotlin.
For multiplatform projects, @JsExport
is available in common code as well. It only has an effect when compiling for the JavaScript target, and allows you to also export Kotlin declarations that are not platform specific.
warning
This feature is Experimental. It may be dropped or changed at any time. Use it only for evaluation purposes. We would appreciate your feedback on it in YouTrack.
The @JsStatic
annotation instructs the compiler to generate additional static methods for the target declaration. This helps you use static members from your Kotlin code directly in JavaScript.
You can apply the @JsStatic
annotation to functions defined in named objects, as well as in companion objects declared inside classes and interfaces. If you use this annotation, the compiler will generate both a static method of the object and an instance method in the object itself. For example:
// Kotlin
class C {
companion object {
@JsStatic
fun callStatic() {}
fun callNonStatic() {}
}
}
Now, the callStatic()
function is static in JavaScript while the callNonStatic()
function is not:
// JavaScript
C.callStatic(); // Works, accessing the static function
C.callNonStatic(); // Error, not a static function in the generated JavaScript
C.Companion.callStatic(); // Instance method remains
C.Companion.callNonStatic(); // The only way it works
It's also possible to apply the @JsStatic
annotation to a property of an object or a companion object, making its getter and setter methods static members in that object or the class containing the companion object.
See how Kotlin types are mapped to JavaScript ones:
Kotlin | JavaScript | Comments |
---|---|---|
|
| |
|
| The number represents the character's code. |
| Not supported | There is no 64-bit integer number type in JavaScript, so it is emulated by a Kotlin class. |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| Carries the property |
|
| |
|
| |
|
| Carries the property |
|
| Carries the property |
|
| Exposes an |
|
| Exposes an ES2015 |
|
| Exposes an ES2015 |
| Undefined | Exportable when used as return type, but not when used as parameter type. |
|
| |
|
| |
Nullable |
| |
All other Kotlin types (except for those marked with | Not supported | Includes Kotlin's unsigned integer types. |
Additionally, it is important to know that:
Kotlin preserves overflow semantics for
kotlin.Int
,kotlin.Byte
,kotlin.Short
,kotlin.Char
andkotlin.Long
.Kotlin cannot distinguish between numeric types at runtime (except for
kotlin.Long
), so the following code works:fun f() { val x: Int = 23 val y: Any = x println(y as Float) }
Kotlin preserves lazy object initialization in JavaScript.
Kotlin does not implement lazy initialization of top-level properties in JavaScript.
Thanks for your feedback!