The class operator: a bridge between object and function exemplers
Dmitry Soshnikov
dmitry.soshnikov at gmail.com
Mon Nov 14 23:52:11 PST 2011
On 15.11.2011 11:50, Dmitry Soshnikov wrote:
> On 15.11.2011 2:01, 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?
>>
>> 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.
>>
>> 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.
>>
>>
>>> /UnaryExpression/ :
>>> *class* /UnaryExpression/
>>> ...
>>>
>>> The semantics are:
>>> 1. if /UnaryExpression/ is undefined or null, return the value of
>>> /UnaryExpression/.
>>> 2. Let /obj/ be ToObject(/UnaryExpression/)
>>> 3. Return the result of calling the [[Get]] internal method of /obj/
>>> with argument 'constructor'
>>
>
> Sorry, perhaps I'm missing something (correct me if I'm wrong), but
> seems this algorithm just not working.
>
> According to examples, yes, the Point is set to the function
> previously referred by temporary created object's `constructor'
> property. But what's then?
>
> Instances created by such a constructor, will have prototype set to
> `Object.prototype' (since it's the value of `Point.prototype' now).
> Therefore, the do not see neither specified `x' and `y' default
> (shared, static) properties, nor any other method placed on that
> temp-object:
>
> You may of course try to set Point.prototype = Point after that in a
> hope that it starts to see the properties, but it's obviously useless
> since Point already is set to temp-object's `constructor' property and
> actually is missed by GC.
>
Woops.. :) (I realized that I have 40 unread mails which I missed, so I
guess it's very likely already was mentioned; sorry in case)
> Dmitry.
>
>> Interesting, so 'constructor' will inherit from Object.prototype if
>> missing from the exemplar. This means
>>
>> let Point = class {
>> x:0,
>> y,0
>> };
>>
>> let p = new Point(1, 2);
>>
>> will result in p being constructed via the equivalent of new Number(1).
>>
>>
>>> The class operator can prefix any UnaryExpression, not just object
>>> literals. That means that the class operator can be used to
>>> classify objects:
>>>
>>> if (class p === Point) ...
>>
>> This is good.
>>
>> It has the same multiple-global issue that instanceof has.
>>
>>
>>> Note that many object-oriented abstraction designers consider this
>>> for of class testing to be an anti-pattern.
>>
>> Yup but it has its uses occasionally.
>>
>>
>>> Relationship between the class and instanceof operators
>>>
>>> There isn't one. They are different operators. class is simply a
>>> short hand for accessing an object's constructor property
>>> instanceof tests a specific inheritance relationship.
>>
>> Oh, but weren't you going to make instanceof work with an object
>> exemplar on the right?
>>
>> /be
>>
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111115/fe66d932/attachment-0001.html>
More information about the es-discuss
mailing list