javascript anonymous object in kotlin

前端 未结 6 1909
终归单人心
终归单人心 2021-02-07 09:18

how to create JavaScript anonymous object in kotlin? i want to create exactly this object to be passed to nodejs app

var header = {“content-type”:”text/plain” ,          


        
6条回答
  •  野的像风
    2021-02-07 09:32

    Here's a helper function to initialize an object with a lambda syntax

    inline fun jsObject(init: dynamic.() -> Unit): dynamic {
        val o = js("{}")
        init(o)
        return o
    }
    

    Usage:

    jsObject {
        foo = "bar"
        baz = 1
    }
    

    Emited javascript code

    var o = {};
    o.foo = 'bar';
    o.baz = 1;
    

提交回复
热议问题