据我所知,你必须声明require()
在 kotlin 中是这样的:
external fun require(module: String): dynamic
用法
val module = require("module")
但是在使用这个的时候,我遇到了这个奇怪的问题:
var myObject = module.SampleClass();
^
TypeError: Class constructor SampleClass cannot be invoked without 'new'
前置词
new
破坏代码。在这种情况下我该怎么办?Link to the object I tried to create
更新
在查看已编译的 javascript 文件时,我意识到了一些事情。
我实际编写的代码
val myObject = module.SampleClass()
编译代码
var myObject = module.SampleClass();
这是有道理的,当实际上没有
new
时会引发此错误。在构造函数调用之前。在这种情况下我应该调用类似关键字或函数的东西吗?
最佳答案
显然,有一个 js(code: String)
功能。正确的方法是使用调用构造函数,如下所示:
val client = js("new module.SampleClass();")
关于javascript - 无法从 Kotlinjs 中的所需模块创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49097351/