The class operator: a bridge between object and function exemplers

Allen Wirfs-Brock allen at wirfs-brock.com
Mon Nov 14 15:01:14 PST 2011


On Nov 14, 2011, at 2:01 PM, Brendan Eich wrote:

> On Nov 14, 2011, at 12:16 PM, Allen Wirfs-Brock wrote:
> 
>> let Point = {
>>      x:0,
>>      y,0,
>>      constructor(x,y} {
>>          this.x=x;
>>          this.y=y;
>>      }
>> }.constructor;   //<----------- note added property reference
>> 
>> let p = new Point(1,2);  //new operator applied to a constructible function 
> 
> So can you spec operator new a bit? It tries [[Construct]] and if missing, tries for a .constructor property that it calls on a fresh instance of Object whose [[Prototype]] is the exemplar?

Yup, that's essentially the semantics. I'm going to write up the object exemplar strawman which will cover this (and also instanceof). 

> 
> Dave's work on http://wiki.ecmascript.org/doku.php?id=strawman:minimal_classes brought to light some added wiring that comes for free in the prototypal pattern when you write a constructor function as a function declaration: the intrinsic name of the constructor function is the class name.
> 
> For exemplars, if constructor is defined using method definition shorthand,
> 
> http://wiki.ecmascript.org/doku.php?id=harmony:object_literals#object_literal_property_shorthands
> 
> says the intrinsic name will be 'constructor'. This will be a drag in debugging scenarios. You'll want to see Point, Square, Circle, Ellipse, Rectangle, etc. but all you'll get is 'constructor' every time.

This is an interesting point that arises for any non-binding declaration form.  Function expression really are a similar situation where the only (semantic) reason for having a name is so it can be intrinsically referenced. In theory, some sort of internal naming syntax could be adopted for this case.  However, I worry that that part leads to more and more embellishment that try to address different minor use cases and usability issues.  We eventually get back to Cordova proposals that we can't get consensus on.  I tend to  think of JS as being more of a non-builtout OEM VW bus rather than a 70's Detroit luxury car.   

Another approach to the debugging problem is the one put forward in the Mirghasem, et al SPLASH/Wavefront paper (http://dl.acm.org/citation.cfm?id=2048222&preflayout=tabs preprint: http://blog.getfirebug.com/2011/04/28/naming-anonymous-javascript-functions/ ) Such constructors would simply be recognized as yet another variation of implicit function naming that debuggers should be able to recognize (perhaps with some front-end help) and assign a meaningful name to.  It's probably a win if tools can deal with it out having to add to the language.

> 
> The attributes in the http://wiki.ecmascript.org/doku.php?id=harmony:object_literals#object_literal_property_shorthands section are frosty. Is that the right default for methods? Is it the right one for constructor in particular? I'm just asking, this is a separate issue but I spotted it and wanted to get it out before forgetting.

We've thrashed around about attributes for concise methods with the latest decision happening at the Sept. TC39 meeting.  I don't know if those decisions are still  holding.  Regardless, I think "constructor" should be considered special and hence potentially could have other attribute defaults.  Another special need of constructor method (in an object literal) is that it needs to have its prototype property automatically set to the object that contains it.  (another detail for the object exemplar strawman).

Allen

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111114/8cb4e554/attachment.html>


More information about the es-discuss mailing list