Public property in class member definition
The proposal is an enhancement of public property of class definition syntax.
The proposal will implementation public
keyword in class definition:
-
Nothing NEW
Concept cleanly. All the features are grammatical sugar, except that the
public
keyword is enabled.
The proposal is not tc39 officailly now but implemented at prepack-core with proposal-public-property (@here).
You could see more test case in this project (@here).
Table of Contents
Syntax design
Key principle:
All the features of grammatical sugar can be implement base on existing.
1. using public
define public property, or default
Ex:
class f {
public data = 100;
public static data = 200;
}
// equal
let f = class {};
let defaultAttr = { writable: true, enumerable: true, configurable: true };
Object.defineProperty(f.prototype, 'data', {value: 100, ...defaultAttr});
Object.defineProperty(f, 'data', {value: 200, ...defaultAttr});
case 2:
class f {
public foo() {
// enabled, can be remove `public`
}
// equal
foo() {
}
}
2. publish member from private scope
example, for private property:
class MyClass {
private x = 100;
public as x;
}
// equal
class MyClass {
private x = 100;
get x() {
return x;
}
set x(v) {
x = v;
}
}
case 2, for protected property:
class MyClass {
protected x = 100;
}
class MyClassEx extends MyClass {
public as x; // accept
}
3. publish with alias
Ex:
class MyClass {
protected x = 100;
}
class MyClassEx extends MyClass {
public x as valueX; // alias `valueX`
}
// equal
class MyClassEx extends MyClass {
get valueX() {
return x;
}
set valueX(v) {
x = v;
}
}
Concepts
The truth is "All the normal properties are published in JavaScript".
Implementation
Core rules:
- Can not publish a no-private member using
public as
syntax. - Can not assignment initialization value when
public as
a member.
Key implementation steps:
- eta
public
keyword when evaluate definition item. - make
get
andset
method for "public [name] as <name>" grammatical.
Done.
Testcases
The proposal has full test case in repository, current syntax based.
# install test framework
> mkdir node_modules
> npm install fancy-test chai mocha --no-save
# test it
> mocha
# (OR)
> bash run.sh
References
- Objections to fields, especially the private field syntax
- The proposal should be rejected!
- My comments at #100
History
2019.09.11 initial release.