简体   繁体   English

在 JavaScript/TypeScript 中使用静态引用继承实例方法

[英]Inheriting Instance Methods with Static References in JavaScript/TypeScript

👋 The following examples use access modifiers from TypeScript , but I think the question should also be relevant for JavaScript developers. 👋 以下示例使用来自 TypeScript 的访问修饰符,但我认为这个问题也应该与 JavaScript 开发人员相关。 Let's say I have some parent class, where I define some necessary members and an implementation of a shared method:假设我有一些父类,我在其中定义了一些必要的成员和一个共享方法的实现:

// ParentClass.ts
export default class ParentClass {
  private static voo;
  private bar;

  constructor(bar) {
    this.bar = bar;
  }

  private commonMethod(baz) {
    doSomething(ParentClass.voo);
    doSomethingElse(this.bar, baz);
  }
}

And then I have some child class, that inherits this behavior:然后我有一些子类,它继承了这种行为:

// ChildClass.ts
export default class ChildClass extends ParentClass {
    voo = "Jake";

    constructor(bar) {
        super(bar)
    }

    public uniqueMethod(ter) {
        doAnotherThing(this.bar);
        this.commonMethod(ter);
    }
}

Of course, when I call commonMethod() inside of ChildClass.uniqueMethod() , it will reference the value of ParentClass.voo , which is undefined.当然,当我在ChildClass.uniqueMethod() commonMethod()时,它将引用未定义的ParentClass.voo的值。 What I would like to happen is that each inheriting child class uses the exact same implementation of that method, but it references the static member of the child class itself.我想要发生的是每个继承子类都使用该方法的完全相同的实现,但它引用子类本身的静态成员。 So when I call ChildClass.uniqueMethod() , commonMethod() will use the value from ChildClass.voo() rather than the parent equivalent.因此,当我调用ChildClass.uniqueMethod()时, commonMethod()将使用ChildClass.voo()中的值而不是父等效项。

One sidesteps this issue entirely by just making voo an instance member, rather than a static member, but let's say that you have some scenario where a static voo is more useful in other ways.一个人完全回避了这个问题,只是让voo成为实例成员,而不是静态成员,但是假设您有一些场景,静态voo在其他方面更有用。

Is such a solution readily available?这样的解决方案是否容易获得? I've posted the solution that I'm currently using as a reply to this question, but I can't help but think there's a more direct solution out there.我已经发布了我目前正在使用的解决方案作为对这个问题的回复,但我不禁认为那里有更直接的解决方案。

This is what I currently have, where I'm attaching an instance accessor, and adjusting the commonMethod() to use that accessor:这是我目前拥有的,我在其中附加了一个实例访问器,并调整commonMethod()以使用该访问器:

// ParentClass.ts
export default class ParentClass {
  private static _voo;
  private bar;

  constructor(bar) {
    this.bar = bar;
  }

  public get voo() {
    return ParentClass._voo;
  }

  private commonMethod(baz) {
    doSomething(this.voo);
    doSomethingElse(this.bar, baz);
  }
}

And then inheriting classes override that accessor:然后继承类覆盖该访问器:

// ChildClass.ts
export default class ChildClass extends ParentClass {
    _voo = "Jake";

    constructor(bar) {
        super(bar)
    }

    public override get voo(){
        return ChildClass._voo
    }

    public uniqueMethod(ter) {
        doAnotherThing(this.bar);
        this.commonMethod(ter);
    }
}

This route doesn't seem very elegant to me, so I'd be very interested to hear feedback or suggestions about better ways to do this.这条路线对我来说似乎不是很优雅,所以我很想听听有关更好方法的反馈或建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM