javascript anonymous object in kotlin

前端 未结 6 1907
终归单人心
终归单人心 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:40

    I'm a Kotlin newbie (though not a newbie developer), I slightly extended answer from @bashor to something looks neater for keys which are valid Java identifiers, but still allows ones which aren't. I tested it with Kotlin 1.0.1.

    @JsName("Object")
    open class Object {
    }
    
    fun jsobject(init: dynamic.() -> Unit): dynamic {
        return Object().apply(init)
    }
    
    header = jsobject {
        validJavaIdentifier = 0.2
        this["content-type"] = "text/plain"
        this["content-length"] = 50
    }
    

提交回复
热议问题