javascript anonymous object in kotlin

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

    Possible solutions:

    1) with js function:

    val header = js("({'content-type':'text/plain' , 'content-length' : 50 ...})") 
    

    note: the parentheses are mandatory

    2) with dynamic:

    val d: dynamic = object{}
    d["content-type"] = "text/plain"
    d["content-length"] = 50
    

    3) with js + dynamic:

    val d = js("({})")
    d["content-type"] = "text/plain"
    d["content-length"] = 50
    

    4) with native declaration:

    native
    class Object {
      nativeGetter
      fun get(prop: String): dynamic = noImpl
    
      nativeSetter
      fun set(prop: String, value: dynamic) {}
    }
    
    fun main(args : Array) {
      var o = Object()
      o["content-type"] = "text/plain"
      o["content-length"] = 50
    }
    

提交回复
热议问题