Class Features Proposals
A status update for TC39, September 2020
Ujjwal Sharma and Daniel Ehrenberg
Igalia ❤ Bloomberg
1
Overview
2
How do they look?
3
| Instance Fields | Instance Methods | Static Fields | Static Methods |
Public | class Trainer { pkmn = “Pikachu” } | class Pokemon { attack() { … } } | class Pokemon { static pkdx = […] } | class Pokemon { static starter() { … } } |
Private | class Trainer { #pkmn = “Raikou” } | class Pokemon { #evolve() { … } } | class Pokemon { static #mobs = […] } | class Pokemon { static #teleport() { … } } |
What’s covered? -- ES5
4
| Instance Fields | Instance Methods | Static Fields | Static Methods |
Public | | | | |
Private | | | | |
What’s covered? -- ES6
5
| Instance Fields | Instance Methods | Static Fields | Static Methods |
Public | | ✅ | | ✅ |
Private | | | | |
What’s covered? -- ES6, class fields
6
| Instance Fields | Instance Methods | Static Fields | Static Methods |
Public | ✅ | ✅ | | ✅ |
Private | ✅ | | | |
What’s covered? -- ES6, class fields, private methods
7
| Instance Fields | Instance Methods | Static Fields | Static Methods |
Public | ✅ | ✅ | | ✅ |
Private | ✅ | ✅ | | |
What’s covered? -- ES6, class fields, private methods,
static class features
8
| Instance Fields | Instance Methods | Static Fields | Static Methods |
Public | ✅ | ✅ | ✅ | ✅ |
Private | ✅ | ✅ | ✅ | ✅ |
Public and Private instance fields
class Pokemon {
name;
#hp = 0;
constructor(name, maxHP) {
this.name = name;
this.#hp = maxHP;
}
damage(dmg) {
this.#hp -= dmg;
}
}
9
Private Methods and Accessors
class Pokemon {
#hp = 0;
#exp = 0;
...
#levelUp() {
this.#hp *= 1.1;
this.#exp = 0;
}
set #experience(exp) {
this.#exp = exp;
if (this.#exp > EXP_REQD) this.#levelUp();
}
}
10
Static class features
class Pokemon {
static leaders = ["Roxanne", "Brawly", "Wattson", ...];
static #starters = ["Torchic", "Mudkip", "Treecko"];
static #assignStarter() {
const starter = Math.floor(Math.random() * 3);
return Pokemon.#starters[starter];
}
}
11
Proposals
12
Current Status
13
Specification Status
14
Implementation Status
15
| Babel | TS | XS | QuickJS | V8 | SM | JSC |
Public Instance Fields | ✅ 7.0+ | ✅ N/A | ✅ | ✅ | ✅ 72 | ✅ 69 | ✅ 14 |
Private Instance Fields | ✅ 7.0+ | ✅ 3.8 | ✅ | ✅ | ✅ 74 | 🏁 tp109 | |
Private Instance Methods | ✅ 7.2/3 | 📆 | ✅ | ✅ | ✅ 84 | 📆 | |
Public Static Fields | ✅ 7.0+ | ✅ N/A | ✅ | ✅ | ✅ 72 | ✅ 75 | 🔬 Bug |
Private Static Fields | ✅ 7.4 | ✅ 3.8 | ✅ | ✅ | ✅ | ⚙ | 🔬 Bug |
Private Static Methods | ✅ 7.6 | 📆 | ✅ | ✅ | ✅ | 📆 | 🔬 |
* excluding non-supporting engines
✅ | Supported | ⚙ | Work in progress |
🏁 | Supported behind a flag | 📆 | Planned |
🔬 | Under review | | |
Testing Status (Class Fields)
16
Testing Status (Private Methods)
17
Testing Status (Static Fields)
18
Future Plans
19
Implementation
20
Specification
21
Advancement
22
Thank you!
23