How to understand the statement 'are only available on instances of the class' in the section about Private instance methods in ES6 classes?

huangapple go评论60阅读模式
英文:

How to understand the statement 'are only available on instances of the class' in the section about Private instance methods in ES6 classes?

问题

根据MDN(我强调的部分):

私有实例方法

与它们的公共对应方法不同,私有实例方法:

  • 仅在类的实例上可用,而不在其 .prototype 属性上可用。

我尝试理解'仅在类的实例上可用,而不在其 .prototype 属性上可用',并编写了以下代码:

class ClassWithPrivateMethod {
  #privateMethod() {
    return 42;
  }
}
const method = new ClassWithPrivateMethod();
// 仅在类的实例上可用
method.#privateMethod
// 不在其 .prototype 属性上可用
ClassWithPrivateMethod.prototype.#privateMethod

私有实例方法无法从外部代码调用,所以为什么说'仅在类的实例上可用'呢?'可用'指的是什么?'其'在'其 .prototype'中是指的ClassWithPrivateMethod还是实例?

英文:

Per MDN(emphasis mine):

> #### Private instance methods
> Unlike their public counterparts, private instance methods:
> - are only available on instances of the class, not on its .prototype property.

I tried to understand 'are only available on instances of the class, not on its .prototype property.' and wrote the following code:

class ClassWithPrivateMethod {
  #privateMethod() {
    return 42;
  }
}
const method = new ClassWithPrivateMethod();
// are only available on instances of the class
method.#privateMethod
// not on its .prototype property
ClassWithPrivateMethod.prototype.#privateMethod

Private instance methods cannot be called from external code, so why does it say 'are only available on instances of the class'? What does 'available' refer to? Does 'its' in 'its .prototype' refer to ClassWithPrivateMethod or instances?

答案1

得分: 0

由于它是私有的,您只能从类的方法内部引用它,所以您的示例在两种用法上都出现错误,因为您尝试在类外部访问该属性。

要看到MDN所做的区别,您必须尝试在类的另一个方法内部使用该方法,因此我添加了一个公共方法。

从那里,您可以看到当在类的实例上调用该方法时,您可以访问该方法。但是,如果您尝试在类原型上调用它,就会出现错误。

class ClassWithPrivateMethod {
  #privateMethod() {
    return 42;
  }
  publicMethod() {
    console.log(this.#privateMethod()); // 可行
    console.log(ClassWithPrivateMethod.prototype.#privateMethod()); // 失败
  }
}

let instance = new ClassWithPrivateMethod();
instance.publicMethod();
英文:

Since it's private, you can only refer to it from within a method of the class, so your example gets errors for both uses since you're trying to access the property outside the class.

To see the distinction that MDN is making, you have to try to use the method from within another method of the class, so I've added a public method.

From there you can see that you can access the method when it's called on an instance of the class. But if you try to call it on the class prototype, it gets an error.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

class ClassWithPrivateMethod {
  #privateMethod() {
    return 42;
  }
  publicMethod() {
    console.log(this.#privateMethod()); // works
    console.log(ClassWithPrivateMethod.prototype.#privateMethod()); // fails
  }
}

let instance = new ClassWithPrivateMethod();
instance.publicMethod();

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月30日 00:00:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定