[英]How do you call JavaScript delete from Kotlin?
I am working with a third party library from Kotlin and one of the things I must do is call delete thing[key]
in order to remove an item from thing. 我正在使用Kotlin的第三方库,我必须做的
delete thing[key]
是调用delete thing[key]
以便从事物中删除项目。 I am unable to figure out how to do this from Kotlin code. 我无法从Kotlin代码中找出如何执行此操作。
I did try js("delete thing[key]")
, but thing is a parameter to a function and is name-mangled by the Kotlin > JavaScript compiler so an exception is thrown when the line is executed. 我确实尝试过
js("delete thing[key]")
,但是thing是函数的参数,并且由Kotlin> JavaScript编译器进行了名称拼写,因此执行该行时会引发异常。 I also tried js("delete ") thing[key]
but unsurprisingly that didn't work either. 我也尝试了
js("delete ") thing[key]
但毫不奇怪,它也不起作用。
For delete
operator You can write: 对于
delete
运算符,您可以编写:
external fun delete(p: dynamic): Boolean = noImpl
//...
delete(thing[key])
For more convenient use I'd added some helpers: 为了更方便的使用,我添加了一些帮助器:
fun delete(thing: dynamic, key: String) {
delete(thing[key])
}
// or
fun String.deleteFrom(d: dynamic) {
delete(d[this])
}
fun test(a: Any, k: String) {
delete(a, k)
k.deleteFrom(a)
k deleteFrom a
}
Note: using the delete operator is not a good practice and it'll leads to deoptimizations in JS VMs 注意:使用delete运算符不是一个好习惯,它会导致JS VM中的去优化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.