有几种方法。首先,您可以创建一个dynamic
对象并在那里设置所需的属性:
val settings: dynamic = Any()
settings.success = { data: dynamic -> console.log(data) }
settings.error = { status: dynamic -> console.log(status) }
jCouch.db("db").create(settings)
(您也可以为数据或状态指定相应的类型)
其次,您可以使用json
功能:
jCouch.db("db").create(json(
"success" to { ... }, // edit note: added missing comma
"error" to { ... }
))
第三,您可以编写类型化的标题。我不知道什么是 Couch API,所以标题看起来大概是这样的:
external interface Db {
fun create(settings: DbCreateSettings)
}
external interface DbResult
external interface DbError
external interface DbCreateSettings {
val success: ((DbResult) -> Unit)?
val error: ((DbError) -> Unit)?
}
fun JQuery.db(name: String): Db = asDynamic().db(name)
fun foo() {
jq.db("name").create(object : DbCreateSettings {
override val success = { it: DbResult -> console.log(it) }
override val error = { it: DbError -> console.log(it) }
})
}
最后,您可以看到Kotlin标准库如何为选项声明标头