JsName

Gives a declaration (a function, a property or a class) specific name in JavaScript.

Since Kotlin

1.0

Gives a declaration (a function, a property or a class) specific name in JavaScript.

This may be useful in the following cases:

  • There are two functions for which the compiler gives same name in JavaScript, you can mark one with @JsName(...) to prevent the compiler from reporting error.

  • You are writing a JavaScript library in Kotlin. The compiler produces mangled names for functions with parameters, which is unnatural for usual JavaScript developer. You can put @JsName(...) on functions you want to be available from JavaScript.

  • For some reason you want to rename declaration, e.g. there's common term in JavaScript for a concept provided by the declaration, which in uncommon in Kotlin.

Example:

class Person(val name: String) {
    fun hello() {
        println("Hello $name!")
    }

    @JsName("helloWithGreeting")
    fun hello(greeting: String) {
        println("$greeting $name!")
    }
}

Since Kotlin

1.1

Specifies JavaScript name for external and imported declarations

Since Kotlin

1.8

Properties

Link copied to clipboard
expect val name: String
Since Kotlin 1.0
actual val name: String

the name which compiler uses both for declaration itself and for all references to the declaration. It's required to denote a valid JavaScript identifier.

Since Kotlin 1.1
actual val name: String
Since Kotlin 1.8