W3cubDocs

/Kotlin

<init>

Platform and version requirements: JS (1.1)
<init>(name: String)
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!")
    }
}

© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-js-name/-init-.html