Is class syntax really necessary ?

Alex Russell alex at dojotoolkit.org
Mon May 23 10:55:14 PDT 2011


On May 23, 2011, at 10:41 AM, Brendan Eich 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
>> }
>> };
>> 
>> Allen and I have discussed this before, so it's not a surprise, but I'm worried that some proposals might omit fixing this on their todo list.
> 
> That's a good (C) but I do not see it addressed. Indeed Bob Nystrom counted occurrences in one codebase and found default property values set in class prototypes.
> 
> Your point seems to be that mutable (object) default values are a mistake, and I tend to agree. But how to exclude this option? Just adding sugar won't prevent such mistakes, indeed it could make the mistake more likely, depending on ergonomics and more imponderable user factors.
> 
> Where and how does the current proposal address (C)?

I don't think it does explicitly. The repair would be a hidden __init__ sort of method that does property default initialization on instances before the user-provided constructor is called.

In talking with Peter and Arv, it sounded like they thought this was the sort of thing we'd be able to hammer out once the strawman reaches proposal (and I agreed), although I do think it's worth calling out explicitly in the strawman too.

Regards

> /be
> 
> 
>> 
>>> Can we agree that these are problems to be solved, if not by classes then by other APIs or special forms?
>>> 
>>> /be
>>> 
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss at mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>> 
>> --
>> Alex Russell
>> slightlyoff at google.com
>> slightlyoff at chromium.org
>> alex at dojotoolkit.org BE03 E88D EABB 2116 CC49 8259 CF78 E242 59C3 9723
>> 
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
> 

--
Alex Russell
slightlyoff at google.com
slightlyoff at chromium.org
alex at dojotoolkit.org BE03 E88D EABB 2116 CC49 8259 CF78 E242 59C3 9723



More information about the es-discuss mailing list