派生类是否更新其父类属性?

时间:2019-01-25 12:09:43

标签: javascript typescript

我有几个类是从父级派生的类,它们都更新了一些值,并且我不知道它们是否具有自己的这些值的版本,或者它们实际上是在导航应用程序且与众不同时更新父级的属性正在使用类,我怀疑是后者。

这是我正在查看的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 }
      }
    }

如果是这种情况,那么我想这是网站正在查看特定代码段的点,然后它会随着过程更新父类?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

每个对象都有自己的一组属性(实例变量),具体取决于声明属性的方式。

如果使用static关键字声明了父属性(就像public访问修饰符一样),则从任何子对象中修改父属性都会在所有子对象中修改该值(因为它已被共享)。

因为在您的情况下它们不是静态的,所以每个子代都有自己的父变量副本,当任何一个子代更改其父变量时,可以安全地进行更改。