tc39 / proposal-class-fields

Orthogonally-informed combination of public and private fields proposals
https://arai-a.github.io/ecma262-compare/?pr=1668
1.72k stars 113 forks source link

An alternative proposal and simple implementation #154

Closed aimingoo closed 5 years ago

aimingoo commented 6 years ago

The project at aimingoo/private-property.

The goal of this proposal is to replace the highly controversial old proposal "proposal-class-fields". The new proposal with a concise implementation and aims to propose and accomplish One objective: SIMPLE!

^^~

Thanks for all.

A showcase, I really hope you can run it in person.

class ClassEx {
  private data: 200,

  private foo() {
    console.log("in instances,", '#data is ' + #data);
  }

  test(x) {
    console.log(#data);
    x#data = 'Hello World!';
    x#foo();
  }
}

// testcases
var obj = new ClassEx;
var more = new ClassEx;
obj.test(more);
ljharb commented 6 years ago

The colon there is a forbidden extension, which is reserved for possible future type annotations, and would conflict with TypeScript and Flow.

Additionally, the asymmetry of using private for declaration and # for access wouldn't be acceptable to some on the committee.

Additionally, the shorthand #data was rejected for now, so all private access would need an explicit receiver for the time being.

aimingoo commented 6 years ago

@lijarb Thanks.

This is not just a proposal about syntax, its core claim is No Fields, and delivery an implementation based on the core concept of object is a collection of properties. This more succinct than the operational lexical context, And provides the possibility for implementations such as protectd property.

For grammar, I think that if can't deny the existing property declaration syntax, we can't deny the rationality of private property using : because the latter is a property. However, I can accept the suggestion to cancel the # shorthand, which looks good.

ljharb commented 6 years ago

whether you use : or =, it’s either per-instance (fields), or its on the prototype (massive footgun).

yw662 commented 6 years ago

even more ugly than original, and does not fix '#'.

149 is much more better than yours.

aimingoo commented 6 years ago

@yw662

No. cannot implement protected property base on #149 , and it explicitly uses symbols and lexical contexts. BUT, class declaration itself has no lexical environment, so the solution must also create and maintain such an environment.

The good side is No fields. ^^.

littledan commented 5 years ago

See the FAQ for an explanation of why we use # in this proposal rather than private.