shama
04/23/2020, 5:13 AM@JsExport
class UIComponent {
val tagName: String = "h1"
}
It creates the following JS:
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!David Eriksson
04/23/2020, 6:38 AMDavid Eriksson
04/23/2020, 6:43 AMSean Keane
04/23/2020, 9:17 AMturansky
04/23/2020, 10:25 AMshama
04/23/2020, 3:36 PM@JsExport
on the val as well as adding open val tagName
and both had the same result.