Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
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. 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. 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.

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.

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is what I currently have, where I'm attaching an instance accessor, and adjusting the commonMethod() to use that accessor:

// 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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda