我有几个类是从父类派生的类,它们都会更新一些值,我不知道它们是否有自己的这些值版本,或者它们实际上是在应用程序导航和不同时更新父类的属性正在使用类,我怀疑是后者。
这是我正在查看的 sideContent,以及与此相关的任何其他属性。
父级:
export abstract class EngagementGraphNode {
public sideContent: IEngagementSideContent;
constructor(json?: IEngagementGraphNode) {
if (json) {
this.id = json.id;
this.name = json.name;
}
}
子组件示例:
export class EngagementProductGroup extends EngagementGraphNode {
constructor() {
super();
this.placeholder = this.getIcon('more');
this.icon = true;
this.type = EngagementType.ProductGroup;
}
setProperties(json: IEngagementGraphNode, color: string): void {
this.id = json.id;
this.sideContent = {
overview: { isLoading: true, title: 'Overview', id: 0 },
members: { isLoading: false, title: 'Engaged Individuals', id: 1, individuals: this.individuals }
}
}
如果是这种情况,那么我猜测网站正在查看该特定的代码,然后随着父类的进行而更新它?
如果您能提供帮助,非常感谢!
最佳答案
每个对象都有自己的一组属性(实例变量),具体取决于属性的声明方式。
如果使用 static
关键字声明父属性(就像 public
访问修饰符),则从任何子对象修改父属性都会修改该属性所有子对象中的值(因为它是共享的)。
因为在您的情况下它们不是静态的,所以每个子级都有自己的父级变量副本,当任何子级更改其父级变量时,这些副本都是安全的。
关于javascript - 派生类会更新其父类属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54365094/