About Javascript

I have known that Kotlin was awesome since I switched to it from java, 2 years ago.
But recently, I have tried to use it for javascript target and I have been blown away by Kotlin…again. :heart_eyes:

It is nice, simple and it works. I’m really impressed (I have been able to write a non trivial piece of javascript code really fast). :sunglasses:


  1. Please document the javascript specific features !
    the documentation for Kotlin:JS is lacking a lot : before you can setup a working project and understand how to make it work, you have to google search a lot of old blog posts and guess a lot. But after you get figured out the following stuff, it is pure awesomeness.
  • Calling a top level Kotlin function from javascript

// in Kotlin
yourPackage.yourFunction()

// in javascript
Kotlin.modules.yourLibrary.yourPackage.yourFunction();

  • using the @Native annotation to use a javascript instance/object/function in Kotlin

// accessing a javascript class

// in there, the javascript qualified type
@native(“math.expression.node.Node”)
// the corresponding javascript constructor (with the right parameters)
open class Node() {
// corresponding signature of the javascript methods you want to use
fun toTex():String
fun transform(action:(node:Node, path:String, parent:Node?)-> Node):Node
fun clone():Node
}

// accessing a subclass of Node
@native(“math.expression.node.SymbolNode”)
class SymbolNode(val name: String):Node()

// using an object
@native(“math”)
object math {
fun parse(string:String):Node
}

// using a val (can’t add method signatures)
@native(“math”)
val math = noImpl

// creating a javascript class in Kotlin
val node = SymbolNode(“Hello”)

// accessing it’s value in Kotlin (the output goes in the javascript console)
println(node.name)

// accessing it’s value (the output goes in the html document)
document.writeln(node.name)

// using a method of a javascript object
val node2 = math.parse(“it really works !”)

  • dynamic (working with javascript objects)

  • js() (calling javascript from kotlin only with constant strings…)

  • jsTypeOf()

  1. Is there a way to use @Native class instances as keys in HashMap ?
    How does hashCode and equal work ? is it possible to overload them ?
    At the moment, I have to use instance.toString() as a key

  2. is there a list of features available for the javascript target ? A roadmap ?

2 Likes

Hi, Lakedaemon!

Thanks for your feedback!

We’ll work on it. Feel free to vote to related issues to get updates.

Generic version right now use string representation, but it’s not good enough.

The general implementations lookups equals_za3rmp$ for equals and hashCode for hashCode.
So if you want to override them in JS code you should add corresponding members to your object.

Most of language future will be available since 1.0.2 except reflection. Sorry, no public roadmap yet.