动态类型

    Kotlin is a statically typed language, which makes it different from the dynamically typed JavaScript. In order to facilitate interoperation with JavaScript code, Kotlin/JS offers the type:

    dynamic 类型基本上关闭了 Kotlin 的类型检测系统:

    • A dynamic value can be assigned to variables of any type, or passed anywhere as a parameter.
    • A dynamic variable can have a value of any type.
    • A function that takes a dynamic parameter can take arguments of any type.

    这段代码会按照原样编译:在生成的 JavaScript 代码中,Kotlin中的 dyn.whatever(1) 变为 dyn.whatever(1)

    当在 dynamic 类型的值上调用 Kotlin 写的函数时,请记住由 Kotlin 到 JavaScript 编译器执行的名字修饰。你可能需要使用 或者 @JsExport 注解为要调用的函数分配明确定义的名称。

    当我们把一个 lambda 表达式传给一个动态调用时,它的所有参数默认都是 dynamic 类型的:

    使用 dynamic 类型值的表达式会按照原样转换为 JavaScript,并且不使用 Kotlin 运算符约定。 支持以下运算符:

    • 二元:+、 、 */%><>=<===!====!==&&||
    • 一元
      • 前置:-、 、 !
    • 赋值:+=-=*=/=%=
    • 索引访问:
      • 读:d[a],多于一个参数会出错

    更多技术说明请参见。