简体   繁体   English

为什么我无法访问同名的父 class 属性?

[英]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.

相关问题 为什么在JS继承中无法从子类实例访问父类的属性? - Why I can't access a property of parent class from child class instance in JS inheritance? 为什么不能通过其原型访问类中“ this”上的属性? - Why can’t I access a property on “this” in a class via its prototype? 为什么不能访问对象的属性? - Why can't I access the property of the object? 为什么构造函数不能访问他原型的属性,但可以访问父原型的属性(原型的原型) - Why constructor function can't access property of his prototype, but can access property of parent prototype (prototype of prototype) 为什么我无法使用jquery访问父项中的数据? - Why can't I access data in parent with jquery? 为什么我不能设置 JavaScript 函数的名称属性? - Why can't I set a JavaScript function's name property? 为什么我不能直接访问对象文字的属性? - Why can't I directly access a property of an object literal? 为什么我不能用一个点访问整数的属性? - Why can't I access a property of an integer with a single dot? 为什么我不能在JavaScript函数中访问此CSS属性? - Why can't I access this CSS property in a JavaScript function? 为什么我不能在我的对象文字中访问this.property? - why can't I access this.property in my object literal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM