Hi! Is there a reason why properties on classes ex...
# javascript
s
Hi! Is there a reason why properties on classes exported to JS are not enumerable? I noticed in 1.4-M1, if I have:
Copy code
@JsExport
class UIComponent {
    val tagName: String = "h1"
}
It creates the following JS:
Copy code
function UIComponent() {
  this._tagName = 'h1';
}
UIComponent.prototype._get_tagName_ = function () {
  return this._tagName;
};
Object.defineProperty(UIComponent.prototype, 'tagName', {
  get: UIComponent.prototype._get_tagName_
});
Consuming the JS with:
const h1 = new UIComponent(); Object.keys(h1);
only gives me the underscore prepended property
["_tagName"]
and
for (let p in h1)
will enumerate through the
_tagName
and
_get_tagName_
properties but skip over the property I want,
tagName
. I was expecting the opposite, where
_tagName
is not enumerable and
tagName
is enumerable. The reason is lots of JS libs rely on
hasOwnProperty()
and being able to enumerate through properties on an object considered its own. My specific use case is
Ember.Component.extend(new UIComponent())
doesn’t work because it cannot enumerate and find the properties I have defined. I think at the very least,
enumerable: true
should be added to
defineProperty()
or even better, the public properties get set in the constructor and the behind the scenes ones use
defineProperty()
and keep the default
enumerable: false
. But maybe there is a reason they are the way they are. Thanks!
d
Very interesting case!
s
What happens if you annotate the val with @JSExport?
t
s
Thanks! I’ll watch that issue. @Sean Keane I tried
@JsExport
on the val as well as adding
open val tagName
and both had the same result.
👌 2