Rationale for TypeError when re-assigning a field in constructor

36 views
Skip to first unread message

Sébastien Doeraene

unread,
Apr 25, 2015, 2:46:49 PM4/25/15
to streng...@googlegroups.com
Hi,

The strawman says this:
Assignments to ‘this’ properties in the constructor throw a TypeError if the property already exists on the object or its prototype chain.

I can understand the "or its prototype chain" part. But I wonder what's the rationale for preventing assignment of a property already existing on the object.

In an inheritance scenario, it can be useful to override the value of a field set by the super constructor with another value. In other words, I'd like to do this:

class A {
  constructor() {
    this.x = 5;
  }
}
class B extends A {
  constructor() {
    super();
    this.x = 10;
  }
}


Why is this forbidden? I guess it's related with flattening field access, but I fail to see exactly what would go wrong if it was allowed.

Cheers,
Sébastien

Erik Arvidsson

unread,
Apr 25, 2015, 5:13:27 PM4/25/15
to Sébastien Doeraene, streng...@googlegroups.com
The case that is important is where there are setters on the prototype
chain. In that case we would expose an object that is not fully
initialized.

At this point our plan is to make `this.x = exp` in a strong
constructor use CreateDataProperty [1] to make sure that no setter is
invoked.

[1] http://people.mozilla.org/~jorendorff/es6-draft.html#sec-createdataproperty
> --
> You received this message because you are subscribed to the Google Groups
> "Strengthen JS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to strengthen-j...@googlegroups.com.
> To post to this group, send email to streng...@googlegroups.com.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/strengthen-js/CAJwkOg4HoVc2YDni0y0XJ_FUOPbQ-%2Bo%2BkqVNiYjv4yeod26zoA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
erik

Sébastien Doeraene

unread,
Apr 25, 2015, 5:23:44 PM4/25/15
to Erik Arvidsson, streng...@googlegroups.com
Hi,

Thanks for your answer. I had not thought about the case of setters, indeed.


At this point our plan is to make `this.x = exp` in a strong constructor use CreateDataProperty [1] to make sure that no setter is invoked.

Does that mean there is some remote hope that my use case above might eventually be allowed?

Cheers,
Sébastien

Andreas Rossberg

unread,
Apr 27, 2015, 10:42:08 AM4/27/15
to Sébastien Doeraene, Erik Arvidsson, streng...@googlegroups.com
On 25 April 2015 at 23:23, Sébastien Doeraene <sjrdo...@gmail.com> wrote:

At this point our plan is to make `this.x = exp` in a strong constructor use CreateDataProperty [1] to make sure that no setter is invoked.

Does that mean there is some remote hope that my use case above might eventually be allowed?

The current semantics clearly is over-conservative. I very much would want to relax it eventually. I'm hoping that this becomes easier once property declarations are added to the language (currently under discussion at TC39).

/Andreas


Reply all
Reply to author
Forward
0 new messages