Do Maximally Minimal classes carry their weight?

Allen Wirfs-Brock allen at
Sun Apr 1 08:53:12 PDT 2012

On Apr 1, 2012, at 2:58 AM, Herby Vojčík wrote:

>> ...
> Max-min classes were presented as the thing that does not prevent class evolution in "more object-initializer-like-way" (it is already unfriendly in making it object-initializer-syntactically bercause of comma-elision, but semantically it still can be) as well as other ways. Adding private names into class block may be the thing that stops this future friendliness.
> It depends on one little detail. Does this code:
> function make () {
>  return new class {
>    private x;
>    constructor (x) { this. at x = "foo"; }
>    xOf (o) { return o. at x; }
>  };
> }
> let obj = make();
> console.log(make().xOf(obj));
> print out "undefined" or "foo"?
> If you agree that it prints out undefined, I will agree with private in classes. If you claim it should print out "foo", I'll say no to this-way-defined private inside class block.

According to my assumptions it would print "undefined".  The intent for private outside of a class declaration is that it would simply be sugar for:
   const id = Name.create();
and that within a ClassBody it has the same semantics, except that it is lexically scoped to the ClassBody.  That means each evaluation of a class definition gets a new instance of the lexically scoped private name id that is initialized to a new private name value. So each:
   new class{ }
is creating a new binding and value for x and each object returned from make will have a different private named property key for x.

> Of course, this prints "foo" (the previous example is about independently created class expressions):
> class FooHolder {
>  private x;
>  constructor (x) { this. at x = "foo"; }
>  xOf (o) { return o. at x; }
> };
> let obj = new FooHolder;
> console.log(new FooHolder().xOf(obj));
> The bottom line is: there is already movement to have "private x" in the language, as the shortcut to _locally_* define x as Name.create(). If 'private x' added to classes will follow this semantics (so it is deifferent for different invocations of class expression, but still the same for (the same) declared class (or for the instances of the same class expression), I will accept it; but I will raise my hand against if it would have different semantics.

>> - Russ
> Herby
> * P.S.: I don't like it, I would rather see 'private x' as 'define static shared one over multiple invocations', but no one liked it that way, so I accepted 'shortcut to let' semantics; nevertheless, I want it to be everywhere consistently.
> _______________________________________________
> es-discuss mailing list
> es-discuss at

More information about the es-discuss mailing list