Is class syntax really necessary ?

Bob Nystrom rnystrom at google.com
Mon May 23 11:25:45 PDT 2011


On Mon, May 23, 2011 at 10:41 AM, Brendan Eich <brendan at mozilla.com> wrote:

> On May 23, 2011, at 10:03 AM, Alex Russell wrote:
>
> >> (A) the boilerplate needed to set up a sub-prototype object with correct
> constructor property, and
> >>
> >> (B) the pain of doing correct super calls by hand.
> >
> > I hope we can add the hazards of incorrectly adding mutable state to a
> prototype and not an instance to this list. I.e., many people get this
> wrong:
> >
> > function C(){
> >  // should include:
> >  //   this._list = [];
> > }
> > C.prototype = {
> >  _list: [],
> >  addToList: function(item) {
> >    this._list.push(item);    // logic error!
> >    // side-effects here
> >  }
> > };
>

There's two bugs there:
1. Not creating this._list in the ctor.
2. Creating one on .prototype.

If bug #1 is fixed, there's no sensible reason to ever define _list on the
prototype since it would always be shadowed.

> Where and how does the current proposal address (C)?

One thing I'd like the proposal to support, which it doesn't currently, is
initializers on instance property declarations. Then you could do:

class C {
  public _list = [];
}


With that, you'll correctly get a new _list on each instance of C when it's
created.

- bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110523/feb8aec0/attachment.html>


More information about the es-discuss mailing list