gpt4 book ai didi

kotlin - kotlin 中的 javascript 匿名对象

转载 作者:IT老高 更新时间:2023-10-28 13:32:06 26 4
gpt4 key购买 nike

如何在 kotlin 中创建 JavaScript 匿名对象?我想准确地创建这个对象以传递给 nodejs 应用程序

var header = {“content-type”:”text/plain” , “content-length” : 50 ...}

最佳答案

可能的解决方案:

1) 带js功能:

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

注意:括号是强制性的

2) 带有动态:

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

3) js + 动态:

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

4) 带有原生声明:

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
}

关于kotlin - kotlin 中的 javascript 匿名对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28150124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com