Is this context valid for calling a callback function with the correct "this" context?
data.on('load', doSomething.call(this));
Or would it be better to use an additional arrow function (yes, I'm using babel to compile.
data.on('load', () => {
doSomething.call(this);
});
Both seem to have the same result in Chrome but haven't checked other browsers. Is there a best practice or is one way better supported than another?
The following will call doSomething immediately, and assign the returned value as the event listener:
data.on('load', doSomething.call(this));
You probably want bind instead:
data.on('load', doSomething.bind(this));
Your arrow function would work too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With