Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!