https://kotlinlang.org logo
#javascript
Title
# javascript
z

zyfuni

03/07/2024, 10:36 AM
Hello, I encounter a problem. In crypto-js-wasm, a function look like this https://github.com/originjs/crypto-js-wasm/blob/596159e168a9cce4858f7000701711f328e0e76c/src/core/hasher.js#L41
Copy code
static _createHelper(SubHasher) {
    let result = (message, cfg) => new SubHasher(cfg).finalize(message);
    result.loadWasm = async () => {
      if (!SubHasher.wasm) {
        await SubHasher.loadWasm();
      }
    };
    result.outputSize = SubHasher.outputSize;

    return result;
  }
And I found a official doc, but it not tell me how to invoke MyClass as Function https://kotlinlang.org/docs/js-interop.html#declare-static-members-of-a-class In JavaScript you can define members either on a prototype or a class itself:
Copy code
function MyClass() { ... }
MyClass.sharedMember = function() { /* implementation */ };
MyClass.prototype.ownMember = function() { /* implementation */ };
There is no such syntax in Kotlin. However, in Kotlin we have
companion
objects. Kotlin treats companion objects of
external
classes in a special way: instead of expecting an object, it assumes members of companion objects to be members of the class itself.
MyClass
from the example above can be described as follows:
Copy code
external class MyClass {
    companion object {
        fun sharedMember()
    }

    fun ownMember()
}
a

Artem Kobzar

03/07/2024, 10:43 AM
Yes, it should work: https://pl.kotl.in/-nS0wFMtD
When you say "run class as a function" what do you mean?
Could you show the expected usage in JS?
z

zyfuni

03/07/2024, 10:49 AM
@Artem Kobzar I expected this
Copy code
//js("globalThis.Foo = function () {console.log('hello')}")
//print hello
MyClass()
//also in crypto-js-wasm
await CryptoJSW.MD5.loadWasm()
CryptoJSW.MD5("hello")
//CryptoJSW just is [result] in first block.
I make some edit in the playground. Maybe you could check changes at the time?
a

Artem Kobzar

03/07/2024, 11:05 AM
z

zyfuni

03/07/2024, 1:10 PM
@Artem Kobzar 👍🏻thank you
🫡 1
4 Views