Search code examples
javascriptprivategetter-setterecmascript-next

Private (#) getter in JavaScript


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?


Solution

  • 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
      }
    }