javascript anonymous object in kotlin

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

    One more possible solution:

    object {
            val `content-type` = "text/plain"
            val `content-length` = 50
    }
    

    It seems that it does not work anymore with escaped variable names.

    0 讨论(0)
  • 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<String>) {
      var o = Object()
      o["content-type"] = "text/plain"
      o["content-length"] = 50
    }
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2021-02-07 09:34

    Here is another solution:
    Define the following helper function

    fun jsObject(vararg pairs: Pair<Any, Any>): dynamic {
        val result = js("({})")
        for ((key, value) in pairs) {
            result[key] = value
        }
        return result
    }
    

    You can then use it as follows

    val header = jsObject("content-type" to "text/plain", "content-length" to 50)
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2021-02-07 09:50

    It is possible to transform a regular Kotlin object into a JavaScript anonymous object using JavaScript's Object.assign(). This allows you to stay in pure Kotlin and in type-safety as long as possible. So:

    fun Any.toJsObject(): dynamic {
        val thisArg = this                       // Allows use in js() function
        return js("Object.assign({},thisArg)")
    }
    
    val anObject = object { val a = "a" }        // Or use a regular class
    console.log( anObject.toJsObject().a )       // logs "a"
    
    0 讨论(0)
提交回复
热议问题