๐Ÿ”ธprivate member

JS โŸฉ value โŸฉ object โŸฉ class โŸฉ member โŸฉ private

  • private members are not inheritedโ—

class A {
  // โญ๏ธ private members     // # means "private"
  #a;                       // this.#a = undefined
  #b = 0;                   // this.#b = 0
  get #c() { return 0 }     // this.#c   (private getter)
  #d() { ... }              // this.#d() (private method)
  
  // โญ๏ธ private static members
  // (accessable only within the class body)
  static #staticProp;                // A.#staticProp
  static get #CONST() { return 0 }   // A.#CONST
  static #staticMethod() { ... }  // A.#staticMethod()
}
  • never use "this" to access a private static field,

  • always use the direct class name.

๐Ÿ‘‰ ๐Ÿ“— 2ality.com โŸฉ ECMAScript proposal: private class fields

supporting environments

Last updated