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

87
Visualizações
How to attach function that uses `this` to instance-object

I'm trying to modify and instance-object's behaviour for testing. If you need to know exactly what I'm trying to achive, it's this:

const fakeWebSocket$$: WebSocketSubject<any> = new Subject<any>() as WebSocketSubject<any>;
fakeWebSocket$$.multiplex = (x: any, y: any, filterFn: (z: any) => boolean) => 
    fakeWebSocket$$.pipe(filter(filterFn));

which is attaching a fake multiplex-method to an RxJS-Subject to be able to emulate a WebSocketSubject.

This works fine, but notice how I have to refer to the fakeWebSocket$$ twice:

// simplified for readability
fakeWebSocket$$.multiplex = (x, y, filterFn) => fakeWebSocket$$.pipe(filter(filterFn));

I'd hope there is a way to replace the second explicit call to fakeWebSocket$$ with this, somehow?

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

0

To be able to use this, you have to rewrite the arrow function to a normal function function:

fakeWebSocket$$.multiplex = function (x, y, filterFn) {
  return this.pipe(filter(filterFn));
};

This usage is not documented, but it should work.

The documentation however is written in such a way that suggests creating a derived class to override multiplex(), rather than assigning a new value directly to the instance:

class FakeSubject<T> extends WebSocketSubject<T> {
  override multiplex(
    subMsg: () => any,
    unsubMsg: () => any,
    messageFilter: (value: T) => boolean
  ) {
    return this.pipe(filter(messageFilter));
  }
}

const fakeWebSocket$$ = new FakeSubject();
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