javascript - 派生类会更新其父类属性吗?

标签 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 }
      }
    }

如果是这种情况,那么我猜测网站正在查看该特定的代码,然后随着父类的进行而更新它?

如果您能提供帮助,非常感谢!

最佳答案

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

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

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

关于javascript - 派生类会更新其父类属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54365094/

相关文章:

javascript - 来自 Ajax 的 Bootstrap Popover

带有自定义组件的 Angular 包裹 Angular Material 选项卡组件

reactjs - 如何使用类似于 JS && Check 的 RenderIf 组件处理 TypeScript 中未定义的值

javascript - 为复杂验证编写正则表达式

javascript - XMLHTTPRequest 错误 : Synchronous XMLHttpRequest on the main thread is deprecated . ..(SoundCloud API)

javascript - req.body.username 未输出正确的值

Node.js/Mongoose/MongoDb Typescript MapReduce - emit() 和 Array.sum() 方法

Angular2 @HostListener 在派生组件中不起作用

javascript - 向 IDE 提供使用 Electron 的 remote.require 导入的模块的 typescript 定义

javascript - one 如何用 JS 跟踪访问者的去向?