JsName

Common
JS
1.0
@Target([AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER]) annotation class JsName
(Common source) (JS source)
For Common

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

For JS

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!")
    }
}

Constructors

Common
JS
1.0

<init>

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

<init>(name: String)

Properties

Common
JS
1.0

name

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.

val name: String