Search code examples
javascriptes2022

why class private property still can be accessed in chrome 114?


class Person {
  #name = 'Ergonomic brand checks for Private Fields';
  aa=1
  static check(obj) {
    return #name in obj;
  }
}

var p = new Person()

p.#name can be accessed! means not supported...

p.#age has error report, means supported... enter image description here


Solution

  • From mdn:

    It is a syntax error to refer to # names from outside of the class. It is also a syntax error to refer to private properties that were not declared in the class body

    And also

    Note: Code run in the Chrome console can access private properties outside the class. This is a DevTools-only relaxation of the JavaScript syntax restriction.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields