Syntax Proposal: Allow Java-like Object Literals after constructor calls to set properties on created objects.

Brendan Eich brendan at mozilla.com
Wed Jun 30 11:13:38 PDT 2010


On Jun 30, 2010, at 10:05 AM, Jürg Lehni wrote:

> I am still interested in hearing more feedback on this. Maybe my examples were not so clear?
> 
> As more real world example, taken from a UI library that I am working with, would look like this:
> 
> 	var stopButton = new ImageButton(this) {
> 		image: getImage('stop.png'),
> 		size: buttonSize,
> 		toolTip: 'Stop Running Scripts',
> 		onClick: stopAll
> 	};
> 
> 
> Again, all the properties from the object literal immediately following the constructor call would then be set on the created object.
> 
> Rhino allows me to use this already and it has been proven to be very useful in many occasions, leading to cleaner and more readable code.

Some of us old-timers were around at Netscape with Norris Boyd when this was designed and added to Rhino. It was not added to SpiderMonkey, though, for not terribly compelling reasons.

I remember being concerned about the ASI issue, which requries a [no LineTerminator here] restricted production. That is still a concern: the TC39 committee doesn't want to add restricted productions without very good reason.

This came up later, via https://bugzilla.mozilla.org/show_bug.cgi?id=253138 -- see https://bugzilla.mozilla.org/show_bug.cgi?id=253138#c7. https://bugzilla.mozilla.org/show_bug.cgi?id=253138#c10 suggests an operator of some sort to "merge" the objects.

Another point to note is that [1,2,3] is not equivalent to new Array(3) {0:1, 1:2, 2:3}, because the array initialiser form users the original value of Array.prototype, and it does not call the current binding of Array as a constructor.

So, more work needed to avoid a restricted production, at least. A linking operator or keyword ("with" a la functional record update) would help.

/be



More information about the es-discuss mailing list