javascript - 如何在 Kotlin 的 JS 接口中使用可选参数调用
问题描述
我在 Kotlin 中有一个 Javascript 桥接方法,例如,
@JavascriptInterface
fun add(a: Int? = 0, b: Int? = 0): Int {
return a + b
}
如果我想使用默认值调用,如何从 web 应用程序 js 调用此方法?
android.add(null, null) // OR
android.add() // OR
android.add(a = 0, b = 0)// OR
要不然是啥?
解决方案
要使用参数的默认值,undefined
应作为参数传递。可以这样做:
android.add() // empty arguments means all of them are undefined so defaults are used
android.add(1) // a == 1, b == undefined so the default value (0) is used
android.add(void 0, 2) // a == undefined and its default (0) is used, b == 2
推荐阅读
- android - 你如何修复滑翔错误?
- python - 如何输入提到的频道并在该频道中发送消息?
- python - 如何制作一个简单的机器人来发送我发送的状态更新。(discord.py)
- mongodb - 将 mongodb 从 4.2 升级到 4.4 失败,出现“...不支持的 WiredTiger 文件版本...”
- android - 在外部存储中创建文件的问题
- c# - 将文件上传到 S3 存储桶:await fileTransferUtility.UploadAsync(filePath, _bucket) 出现空引用异常
- javascript - 没有在材质对话框组件上调用角度材质主题
- .net - VB.NET 继承 IntelliSense 幻象成员
- c# - 在 Func 中通过 Lembda 传递子类型
- javascript - Laravel - 使用来自数据库功能改进的数据填充图表