Writing javascript applications with Kotlin

2019-03-14 16:03发布

问题:

I recently started to have a look at Kotlin and managed to create my first JVM applications. It's so cool to have a single language that compiles both to Java and JS. So, now I started playing with Kotlin2js and tried to understand the Javascript interoperability, and the possibilities to use JS frameworks like jQuery.

I found a couple of blog posts and examples: http://blog.jetbrains.com/kotlin/2013/10/writing-kotlin-in-the-browser/ http://blog.jetbrains.com/kotlin/2014/12/javascript-interop/ https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/kotlin2JsProject/mainProject/src/main/kotlin/example/main.kt

Is there a documentation of the Kotlin JS library? It's not yet mentioned at http://kotlinlang.org/api/latest/jvm/stdlib/index.html

I first compiled a simple sample app which used import kotlin.browser.* with gradle as build system which (finally and with some help here worked, thanks again!). Than I imported the project into IDEA and suddenly it didn't compile anymore, I had to change the import to import js.dom.html.*. So, I guess it uses a different version of library? (And IDEA added apply plugin: 'kotlin' to my build.gradle in addition to kotlin2js and I guess this doesn't work.)

IDEA copied kotlin-jslib.jar to lib which says it's "Implementation-Version: 0.7.270" in its Manifest. For the compilation with gradle I used kotlin 1.0.1-1 and I'm pretty sure that I also selected this version in IDEA when creating the project.

So, what are the best sources of information to understand Kotlin2js and the Kotlin-js-lib? Especially the Javascript interoperability, how to use Frameworks like jQuery (it seems that there is jQuery support in the kotlin-js-lib), but also, how I can use other frameworks that don't come with Kotlin support yet. I understood that Kotlin has the dynamic keyword, and http://blog.jetbrains.com/kotlin/2014/12/javascript-interop/ mentioned noImpl which lead to a compile error when I tried to use it. Maybe the best way for now is to look at the Kotlin sources?

Well, this is a rather long and unstructured question covering several aspects, but that's my current state of learning Kotlin :-) And maybe others experience the same problem.

回答1:

Meanwhile Kotlin/JS is available in version 1.2 and there is an introduction, tutorial and reference on the official website.



回答2:

if you're interested to learn about using Kotlin in Node.js app, I've put together a starter project that running a node.js server written in Kotlin. https://github.com/techprd/kotlin_node_js_seed

The aim of this project is to write a full stack web application written entirely with Kotlin JS

let me know what you guys think about this...