18Upcoming Class Features
In this chapter, you'll learn about upcoming
class
features that are almost certain to be in ES2021, and which are stable enough (or nearly stable enough) to use with transpilation today (or even without, in some cases, in modern environments): class fields; private fields and methods/accessors; and static fields and private static methods/accessors.
PUBLIC AND PRIVATE CLASS FIELDS, METHODS, AND ACCESSORS
ES2015's
class
syntax was intentionally just a starting point. Multiple proposals that are likely to be adopted in ES2021 extend it with additional useful features:
- Public field (property) definitions
- Private fields
- Private instance methods and accessors
- Public static fields
- Private static fields
- Private static methods
These make the already-useful1 class
syntax markedly more useful, taking tasks that used to require hiding things in closures, assignments after-the-fact, behind-the-scenes use of WeakMap
s, awkward syntax, etc., and making them simple syntax, and potentially improving the ways JavaScript engines can optimize the results as well.
The enhancements are spread across multiple proposals, which may advance at different rates, ...
Get JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.