Are private getters/setters planned to be supported in JavaScript?
class Next {
#private = 0
get #computed() { // SyntaxError: Unexpected token (
return this.#private + 1
}
}
If not, what is the rationale behind that?
I suppose implementation wouldn't be a hurdle. Are there objections to the functionality itself?
Update - ECMAScript 2021
With the latest es2021 version Private getters and setters are also possible.
Your code should be valid now:
class Next {
#private = 0
get #computed() {
return this.#private + 1
}
}
Yes, they're part of the private methods and accessors proposal, a follow-on to class fields. The syntax is exactly as you've shown it. JavaScript engines are actively working to implement them, and Babel has working transpilation for them via the @babel/plugin-proposal-private-methods plugin.
Those two proposals are joined by the static class features proposal which covers static public properties, static private fields, and static private methods (including accessors).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With