原文译文操作

Kotlin was designed for easy interoperation with Java platform. It sees Java classes as Kotlin classes, and Java sees Kotlin classes as Java classes. However, JavaScript is a dynamically-typed language, which means it does not check types in compile-time. You can freely talk to JavaScript from Kotlin via dynamic types, but if you want the full power of Kotlin type system, you can create Kotlin headers for JavaScript libraries.

Inline JavaScript

You can inline some JavaScript code into your Kotlin code using the js("…") function. For example:

fun jsTypeOf(o: Any): String {
    return js("typeof o")
}

The parameter of js is required to be a string constant. So, the following code is incorrect:

fun jsTypeOf(o: Any): String {
    return js(getTypeof() + " o") // error reported here
}
fun getTypeof() = "typeof"

external modifier

To tell Kotlin that a certain declaration is written in pure JavaScript, you should mark it with external modifier. When the compiler sees such a declaration, it assumes that the implementation for the corresponding class, function or property is provided by the developer, and therefore does not try to generate any JavaScript code from the declaration. This means that you should omit bodies of external declarations. For example:

external fun alert(message: Any?): Unit

external class Node {
    val firstChild: Node

    fun append(child: Node): Node

    fun removeChild(child: Node): Node

    // etc
}

external val window: Window

Note that external modifier is inherited by nested declarations, i.e. in Node class we do not put externalbefore member functions and properties.

The external modifier is only allowed on package-level declarations. You can't declare an external member of a non-external class.

Kotlin 已被设计为能够与 Java 平台轻松互操作。它将 Java 类视为 Kotlin 类,并且 Java 也将 Kotlin 类视为 Java 类。但是,JavaScript 是一种动态类型语言,这意味着它不会在编译期检查类型。你可以通过动态类型在 Kotlin 中自由地与 JavaScript 交流,但是如果你想要 Kotlin 类型系统的全部威力 ,你可以为 JavaScript 库创建 Kotlin 头文件。

内联 JavaScript

你可以使用 js("……") 函数将一些 JavaScript 代码嵌入到 Kotlin 代码中。 例如:

fun jsTypeOf(o: Any): String {
    return js("typeof o")
}

js 的参数必须是字符串常量。因此,以下代码是不正确的:

fun jsTypeOf(o: Any): String {
    return js(getTypeof() + " o") // 此处报错
}
fun getTypeof() = "typeof"

external 修饰符

要告诉 Kotlin 某个声明是用纯 JavaScript 编写的,你应该用 external 修饰符来标记它。 当编译器看到这样的声明时,它假定相应类、函数或属性的实现由开发人员提供,因此不会尝试从声明中生成任何 JavaScript 代码。 这意味着你应该省略 external 声明内容的代码体。例如:

external fun alert(message: Any?): Unit

external class Node {
    val firstChild: Node

    fun append(child: Node): Node

    fun removeChild(child: Node): Node

    // 等等
}

external val window: Window

请注意,嵌套的声明会继承 external 修饰符,即在 Node 类中,我们在成员函数和属性之前并不放置 external

external 修饰符只允许在包级声明中使用。 你不能声明一个非 external 类的 external 成员。

纠正翻译

Declaring (static) members of a class

In JavaScript you can define members either on a prototype or a class itself. I.e.:

function MyClass() {
}
MyClass.sharedMember = function() { /* implementation */ };
MyClass.prototype.ownMember = function() { /* implementation */ };

There's no such syntax in Kotlin. However, in Kotlin we have companion objects. Kotlin treats companion objects of external class in a special way: instead of expecting an object, it assumes members of companion objects to be members of the class itself. To describe MyClass from the example above, you can write:

external class MyClass {
    companion object {
        fun sharedMember()
    }

    fun ownMember()
}

Declaring optional parameters

An external function can have optional parameters. How the JavaScript implementation actually computes default values for these parameters, is unknown to Kotlin, thus it's impossible to use the usual syntax to declare such parameters in Kotlin. You should use the following syntax:

external fun myFunWithOptionalArgs(x: Int,
    y: String = definedExternally,
    z: Long = definedExternally)

This means you can call myFunWithOptionalArgs with one required argument and two optional arguments (their default values are calculated by some JavaScript code).

Extending JavaScript classes

You can easily extend JavaScript classes as they were Kotlin classes. Just define an external class and extend it by non-external class. For example:

external open class HTMLElement : Element() {
    /* members */
}

class CustomElement : HTMLElement() {
    fun foo() {
        alert("bar")
    }
}

There are some limitations:

  1. When a function of external base class is overloaded by signature, you can't override it in a derived class.
  2. You can't override a function with default arguments.

声明类的(静态)成员

在 JavaScript 中,你可以在原型或者类本身上定义成员。即:

function MyClass() {
}
MyClass.sharedMember = function() { /* 实现 */ };
MyClass.prototype.ownMember = function() { /* 实现 */ };

Kotlin 中没有这样的语法。然而,在 Kotlin 中我们有伴生(companion)对象。Kotlin 以特殊的方式处理external 类的伴生对象:替代期待一个对象的是,它假定伴生对象的成员就是该类自身的成员。要描述来自上例中的 MyClass,你可以这样写:

external class MyClass {
    companion object {
        fun sharedMember()
    }

    fun ownMember()
}

声明可选参数

一个外部函数可以有可选参数。 JavaScript 实现实际上如何计算这些参数的默认值,是 Kotlin 所不知道的, 因此在 Kotlin 中不可能使用通常的语法声明这些参数。 你应该使用以下语法:

external fun myFunWithOptionalArgs(x: Int,
    y: String = definedExternally,
    z: Long = definedExternally)

这意味着你可以使用一个必需参数和两个可选参数来调用 myFunWithOptionalArgs(它们的默认值由一些 JavaScript 代码算出)。

扩展 JavaScript 类

你可以轻松扩展 JavaScript 类,因为它们是 Kotlin 类。只需定义一个 external 类并用非 external 类扩展它。例如:

external open class HTMLElement : Element() {
    /* 成员 */
}

class CustomElement : HTMLElement() {
    fun foo() {
        alert("bar")
    }
}

有一些限制:

  1. 当一个外部基类的函数被签名重载时,不能在派生类中覆盖它。
  2. 不能覆盖一个使用默认参数的函数。
纠正翻译

Note that you can't extend a non-external class by external classes.

external interfaces

JavaScript does not have the concept of interfaces. When a function expects its parameter to support foo and bar methods, you just pass objects that actually have these methods. You can use interfaces to express this for statically-typed Kotlin, for example:

external interface HasFooAndBar {
    fun foo()

    fun bar()
}

external fun myFunction(p: HasFooAndBar)

Another use case for external interfaces is to describe settings objects. For example:

external interface JQueryAjaxSettings {
    var async: Boolean

    var cache: Boolean

    var complete: (JQueryXHR, String) -> Unit

    // etc
}

fun JQueryAjaxSettings(): JQueryAjaxSettings = js("{}")

external class JQuery {
    companion object {
        fun get(settings: JQueryAjaxSettings): JQueryXHR
    }
}

fun sendQuery() {
    JQuery.get(JQueryAjaxSettings().apply {
        complete = { (xhr, data) ->
            window.alert("Request complete")
        }
    })
}

External interfaces have some restrictions:

  1. They can't be used on the right hand side of is checks.
  2. as cast to external interface always succeeds (and produces a warning in compile-time).
  3. They can't be passed as reified type arguments.
  4. Then can't be used in class literal expression (i.e. I::class).

请注意,你无法用外部类扩展非外部类。

external 接口

JavaScript 没有接口的概念。当函数期望其参数支持 foo 和 bar 方法时,只需传递实际具有这些方法的对象。 对于静态类型的 Kotlin,你可以使用接口来表达这点,例如:

external interface HasFooAndBar {
    fun foo()

    fun bar()
}

external fun myFunction(p: HasFooAndBar)

外部接口的另一个使用场景是描述设置对象。例如:

external interface JQueryAjaxSettings {
    var async: Boolean

    var cache: Boolean

    var complete: (JQueryXHR, String) -> Unit

    // 等等
}

fun JQueryAjaxSettings(): JQueryAjaxSettings = js("{}")

external class JQuery {
    companion object {
        fun get(settings: JQueryAjaxSettings): JQueryXHR
    }
}

fun sendQuery() {
    JQuery.get(JQueryAjaxSettings().apply {
        complete = { (xhr, data) ->
            window.alert("Request complete")
        }
    })
}

外部接口有一些限制:

  1. 它们不能在 is 检查的右侧使用。
  2. as 转换为外部接口总是成功(并在编译时产生警告)。
  3. 它们不能作为具体化类型参数传递。
  4. 它们不能用在类的字面值表达式(即 I::class)中。
纠正翻译