Tuesday, May 5, 2020

We continue with our discussion on Java versus Kotlin
There were a few requirements of invoking JavaScript in languages like Kotlin that doesn’t change. These include:
Specifying keywords to denote external code implementation in JavaScript
Allowing inheritance and static membership.
Ability to proxy and extend the implementation with extension classes by giving it the same level of representation as first class citizens of Kotlin.
Ability to bridge the programming paradigm of Javascript with the functional languages. For example, Kotlin does not support dynamic types when targeting ‘jvm’ plugin type
Ability to perform code access security when running in server mode
Ability to maintain strict level for JavaScript consistency and canonicalization of logic expressed in JavaScript
Support for the numerous frameworks and plug-ins that JavaScript comes with. For example, Json with padding provides a method to request data from a server in a different domain. This was previously prohibhited to ensure same origin policy but has since been addressed with Cross-Origin resource sharing (CORS). This way JsonP helps with script element injection or reuse of an existing one. CORS may improve security but JsonP improves functionality providing different purposes and need to run with the language support.
JavaScript dependencies are managed by npm which is a public repository with tools to download the packages. Kotlin allows us to declare these dependencies in the build script and leverages the Yarn package manager to download the packages. It also generates a webpack module bundler configuration 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

No comments:

Post a Comment