So in a browser I receive some JSON in a string th...
# javascript
h
So in a browser I receive some JSON in a string through a websocket, and I want to pass it into the constructor that I posted here yesterday. Actually, I overload the constructor (the code is in an mpp and this works fine on the jvm), so it can receive either a string or a map (but not nothing), but I can't seem to get in touch with those constructors. The object I want to construct is a
Message
, and the correct way to go about it in js is
new Message.constructor()
, right? But I cannot make it use my overloaded constructors, they never seem to get called. Must I name them with annotations in order to use them? (Like
@JsName("MessageFromString")
and
@JsName("MessageFromMap")
or something?)
d
Secondary constructors are called the same way as primary cosntructors:
val newInstance = ClassName(parameters)
Actually, wait, you are trying to do this from JS? I think you will get mangled names then, because JS has no overloading.
h
Exactly. I had to
@JsName
them.
... But JS also has this very particular
ClassName.constructor()
syntax that got me wondering for a while ... (giving up on that path proved very rewarding.)
d
You should just be able to write
new ClassName
in JS 🤔
h
I agree. That was my primitive, intuitive first understanding of it, and in an mpp in particular, use of the public functions and objects in the library should be as similar as possible (ideally identical) on different platforms.
But then again, this
.constructor()
thing in JS is maybe a convention that should be observed ... so, all in all, I succeeded in doing what I wanted, although I do not fully understand why
new ClassName()
doesn't work, and I am far off to other stuff now.