JsName

@Target([AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER]) annotation actual class JsName (source)

Platform and version requirements: 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

<init>

JsName(actual name: String)

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

Properties

name

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.