I am wondering about what is the difference between both approaches.
I see that dom.nim uses ref object`with `{.importc.} pragma.
On the other hand I have used JsObject just adding keys and values. I was trying to use dom's approach, but I am struggling a bit.
So what is preferred? what are the main differences?
ref object of JsRoot
JsObject is kinda too generic too abstract, is like using "auto" everywhere.
Thanks a lot.
One more topic is that, in some cases the properties are predefined in the object definition:
Selection* {.importc.} = ref object
## see `docs<https://developer.mozilla.org/en-US/docs/Web/API/Selection>`_
anchorNode*: Node
anchorOffset*: int
focusNode*: Node
focusOffset*: int
isCollapsed*: bool
rangeCount*: int
`type`*: cstring
I think this is desirable in particular when you are reading the code. But I think this is generating problems in my code. When I create an instance, those properties not assigned get a null in the JS code and the library that I am wrapping gets confused.
Is there a way to tell not to export the properties with no value assigned?
If I have the following code:
import jsffi
type
A = ref object of JsObject
var a = A()
a["name"] = cstring"Joe"
I get the following:
On the other hand, the following code:
import jsffi
type
B = ref object of JsRoot
name:cstring
surname:cstring
male:bool
proc newB(name:string = ""):B =
new result
result.name = name.cstring
var b = B(name:"Joe")
produce the following:
I was wondering if we could instruct Nim only to export the symbols for which there is an explicit assignment.
The preferred style is the second case. It gives you a clear view of the properties available for that type. But the presence of surname = null and male = false might create problems in some cases.