[英]Why can't I access to the parent class property with the same name?
I have two classes A and B in Javascript, A extends B and they both have a property with the same name in their constructor.我在 Javascript 中有两个类 A 和 B,A 扩展了 B,它们在构造函数中都有一个同名的属性。
I am trying to access to the value of property name
from class B, but it is replaced by A's when creating a new object from A.我正在尝试从 class B 访问属性name
的值,但是在从 A 创建新的 object 时,它被 A 替换。
class B { constructor(){ this.name = "I AM B" } speakB(){ return this.name } } class A extends B { constructor(){ super() this.name = "I AM A" } speakA(){ return this.speakB() + " and " + this.name; } } var b = new B(); console.log(b.speakB()) //I AM B var a = new A(); console.log(a.speakA()) //I AM A and I AM A
As expected result I want to get I AM B and I AM A
but I am getting I AM A and I AM A
.正如预期的结果,我想得到I AM B and I AM A
但我得到I AM A and I AM A
。 I also tried with super.speakB() + " and " + this.name;
我也试过super.speakB() + " and " + this.name;
but with no success.但没有成功。
Is there something I am missing when declaring the classes?声明课程时我是否遗漏了什么?
When you extend a class with a derived class and then instantiate the derived class, there's only ONE object there.当您使用派生的 class 扩展 class 然后实例化派生的 class 时,那里只有一个 ZA8CFDE6331C49EB2AC96F9B8。 this.name
refers to the same this
and the same property on that object no matter which class the code is in. this.name
指的是与 object 相同的this
和相同的属性,无论代码在哪个 class 中。
They key here is that there's only one object that the base and derived class are both setting properties on.这里的关键是只有一个 object 基础和派生 class 都在设置属性。 So, if you're using the same named property, it will be the same value no matter which base or derived class is setting it.因此,如果您使用相同的命名属性,则无论是哪个基或派生 class 设置它,它都将是相同的值。
Think of it this way:这样想:
let obj = new A();
obj.name = "Bob";
There's only one object with one .name
property.只有一个 object 具有一个.name
属性。 The methods of both A and B both operate on the same object so using the same property name in methods A and B will be setting the exact same property. A 和 B 的方法都在同一个 object 上运行,因此在方法 A 和 B 中使用相同的属性名称将设置完全相同的属性。
When you do this:当你这样做时:
this.name = "I AM A".
in the derived class, you're overwriting the previous value that the parent B set for the .name
property.在派生的 class 中,您将覆盖父 B 为.name
属性设置的先前值。
It means that I cannot use the same property name for both classes?这意味着我不能对两个类使用相同的属性名称?
Correct, not if you intend for the two properties to have separate values.正确,如果您打算让两个属性具有单独的值,则不是。 Note, there are also many cases where a derived class wants to access the properties that the base class sets so this is a feature too.注意,在很多情况下,派生的 class 想要访问基础 class 设置的属性,所以这也是一个特性。 But, if you intend for two properties to be separate for base and derived, then you must give them different names.但是,如果您打算将两个属性分别用于基类和派生类,则必须为它们指定不同的名称。
One possible source of confusion for some people is that methods declared via the class
syntax are kept separate for each class.对于某些人来说,一个可能的混淆来源是通过class
语法声明的方法对于每个 class 是分开的。 Each class has its own prototype object and the prototypes are searched in a chain to resolve methods.每个 class 都有自己的原型 object 并在链中搜索原型以解决方法。 This does allow you to specifically call a base class method, even though both base class and derived have defined the same named method.这确实允许您专门调用基本 class 方法,即使基本 class 和派生方法都定义了相同的命名方法。
But, this is not true for regular instance property assignments this.x = "foo"
as those are assigned to the instance object, not on the prototype object.但是,对于常规实例属性分配this.x = "foo"
则不是这样,因为这些属性分配给实例 object,而不是原型 object。 Remember, the prototype is shared across all object instances so you can't store instance state on the prototype.请记住,原型在所有 object 实例之间共享,因此您不能在原型上存储实例 state。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.