Are private getters/setters planned to be supported in JavaScript?
class Next {
#private = 0
get #puted() { // SyntaxError: Unexpected token (
return this.#private + 1
}
}
Are private getters/setters planned to be supported in JavaScript?
class Next {
#private = 0
get #puted() { // 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?
2 Answers
Reset to default 12Update - 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 #puted() {
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).