The class operator: a bridge between object and function exemplers

Russell Leggett russell.leggett at gmail.com
Thu Nov 17 11:20:44 PST 2011


On Thu, Nov 17, 2011 at 12:04 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>
 wrote:

> OK, I have a fix for the missing constructor problem. See:
> http://wiki.ecmascript.org/doku.php?id=strawman:class_operator#missing_constructors
>
>
>
The biggest thing I see missing is a declarative form that includes the
name. I just think its so close, that to not include a form

    class Point {...}

Which can desugar to create a var Point at the very least, would be a bit
of a tragedy. I think most people will see the class operator and think
about it as analogous to function.

Another piece which might not be as big a deal is that [[ctor]] is not the
same as 'constructor', and in the cases with a missing 'constructor', the
result of 'class Proto' would seem like it makes (or fetches) a function
that behaves like a proper constructor function, but it will result in an
instance whose prototype does not have an own 'constructor' property. (I
think I even confused myself with that, sorry)

The alternative proposal I've been advocating that always creates a new
function and prototype makes tradeoffs in the other direction. It does not
require [[ctor]], or automatic .prototype references from constructor to
the enclosing object literal. It also makes it trivial to include a name on
the constructor function without mutation (because a new function is being
made). OTOH, because it always creates a new function with a fresh
prototype that points up to *UnaryExpression*, you get this important
difference:

    let f = new Foo();
    let isSame = (class f === Foo);
    //isSame is true for allen's proposal, but false for mine.
    //however, mine would behaviorally be the same (barring serious
inspection)

At this point, if Allen's proposal included a declaration form 'class Point
{...}' I would be ready to get behind it. I still have a fondness for mine,
which is used purely for defining classes, but for most cases, his would
work equally well in the ways I would want to use it.

- Russ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111117/c98c02c3/attachment.html>


More information about the es-discuss mailing list