如何访问JavaScript中super-class的私有成员?

How to access the super-class's private member in JavaScript?

class 中,我如何访问它的基础-class 的私有字段,比如说 #property

class Base {
  #property = '1.618'
  toString() {
    return Base.name
  }
}
class X extends Base {
  thisWorks() {
    return super.toString()
  }
  toString() {
    return super.#property // SyntaxError: Unexpected private field
  }
}
console.log(`${new X}`)

在 OOP 中,您无法访问 private 方法或 class 之外的 属性,即使您进行扩展也是如此。但是您可以在子 class.

中访问父 class 的 protected 方法

impossible:

It means that private fields are purely internal: no JS code outside of a class can detect or affect the existence, name, or value of any private field of instances of said class without directly inspecting the class's source, unless the class chooses to reveal them. (This includes subclasses and superclasses.)

Base 将不得不以其他方式故意公开其 #property,例如通过某种方法。