1

I would like to intercept all instance creations of EventSource. I tried the following code:

EventSource.prototype.constructor = function(url, config) {
    EventSource(url, config);
    console.log("intercepted");
}

to override the constructor. It does not work. But the following does work:

EventSource.prototype.sayHi = () => alert("Hi");
new EventSource("my-data").sayHi()

How can I achieve something similar for the constructor/in general instance creation?

I tried searching on the internet and the above code.

1 Answer 1

1

You could override the class itself:

class MyEventSource extends EventSource{
  constructor(...args){
    super(...args);
    console.log('EventSource created');
  }
}

window.EventSource = MyEventSource;

new EventSource("my-data")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.