argument icon indicating copy to clipboard operation
argument copied to clipboard

Argument with default value is not enumerable

Open olegstepura opened this issue 6 years ago • 3 comments

Hi! We are using "@ember-decorators/argument": "0.8.21", and noticed that if we have a default value for an argument annotated with @argument keyword, that property becomes non-enumerable.

We need all own properties to be enumerable to support converting ember object to simple javascript object for storing in localstorage. For this we do Object.keys on an ember object and recursively serialize object to a plain JS object.

So if we do:

  /**
   * @type {number}
   */
  @argument('number')
  amount = 0;

we see that this property is not part of Object.keys. If we log this object in chrome console this property is greyed out. If we simply remove default value like this:

  /**
   * @type {number}
   */
  @argument('number')
  amount;

all starts to work fine.

Same with optional value, this code also leads to property not being enumerable:

  /**
   * @type {number}
   */
  @argument(optional('number'))
  amount = 0;

olegstepura avatar Jul 17 '19 16:07 olegstepura

Unfortunately any decorated property that assigns a getter/setter to the property will not be enumerable. This is also going to be an issue with @tracked in Ember in general, and is an issue for computed properties 😕 I'm not sure what a great solution would be here that would also be performant. This may be something to discuss with decorators in general.

pzuraq avatar Jul 18 '19 22:07 pzuraq

Well, the point is:

  • it is enumerable until it has default value is set... Meaning it can have @argument annotation and still be enumerable.
  • I can enumerate properties that use other annotations as well: @computed or @alias. Did not try it with @tracked

I wonder what does Chrome console use to display such object - as I already said they display this property greyed out.

olegstepura avatar Jul 19 '19 08:07 olegstepura

It shouldn't be possible to enumerate properties with @computed, unless you're enumerating on the prototype? I'm not sure how that would work. Here's a twiddle that demonstrates that, it isn't enumerating the properties: https://ember-twiddle.com/b2f668d0ed862eb1490e155120853f15?fileTreeShown=false

pzuraq avatar Jul 19 '19 18:07 pzuraq