for browser targets. Directories can be distributed by specifying it in the build script.
When JavaScript is called from Kotlin function, it can be inlined with the js(…) function. Other than that, all member functions, optional parameters, classes and interfaces can be written in pure JavaScript and marked with the external modifier. The compiler skips these declarations for generating JavaScript code and the onus on the developer to provide the necessary implementation.
JavaScript does not have the concept of interfaces. Instead JavaScript objects with those methods are passed to the code. A good example for the application of this pattern is the settings object.
The reverse is also made popular with Kotlin being called from JavaScript. For this purpose, Kotlin creates an object that contains all the declarations from the current module.
The KotlinJS module allows generated functions and attributes in JavaScript code to have specific names when the generated names are unfriendly. This is done with the help of @JsName annotation. This annotation parameters takes a string literal
Kotlin has some direct mappings between its basic types and those in JavaScript. For example, a String and an Array map directly to the JavaScript. Primitive array translation is mapped to JavaScript TypedArray in more recent versions.
The compilation of Kotlin projects to Javascript modules is supported in one of four options:
1) Plain when there is no compilation for any module system
2) Asynchronous module definition which is used by require.js library
3) CommonJS convention which is used by npm and involves both require function and module.exports object
4) Unified Module Definitions which is compatible with both AMD and common JS.
This ‘moduleKind’ option is specified in the build.gradle in the ‘compileKotlinJs’ task.
Just like Javascript libraries can export multiple packages from within a module, Kotlin also allows multiple packages to be exported but each package will require a new source file with .kt extension.
Finally, Kotlin also supports a JsNonModule notation which allows the code to be imported in non-module environments such as in Project’s static resources.
No comments:
Post a Comment