It seems that methods are not compiled to method calls in the javascript backend. I am sure there are very good reasons of incompatible semantics here. Yet there are js APIs that require being able to pass objects with methods to library functions. How would one generate the equivalent of this js object?
var obj = {
name: "Andrea",
surname: "Ferretti",
fullname: function() { return this.name + " " + this.surname; },
greet: function() { console.log("Hello ," + this.fullname()); }
};
obj.greet();
In general methods can be passed as closure members, but these do not have access to this
Don't ask me how I know that...
Voila:
# nim js -d:nodejs -d:release --hints:off
type
Obj {.exportc.} = object
name: cstring
surname: cstring
fullname: proc(): cstring
greet: proc()
proc `&`(a,b: cstring): cstring {.importcpp: "(#) + (#)".}
var o {.exportc.}: Obj
o.name = "Hans"
o.surname = "Raaf"
o.fullname = proc (): cstring =
var this {.importc,nodecl.}: Obj
return this.name & " " & this.surname
o.greet = proc () =
var this {.importc,nodecl.}: Obj
let txt: cstring = "Hello " & this.fullname()
{.emit: "console.log(`txt`)\n".}
o.greet()
# or
{.emit: "o.greet()\n".}