I've come across some code that has this sort of pattern in numerous places:
this.someFunction.call(this, param);
but it seems to me just a more verbose way of typing
this.someFunction(param)
The pattern sometimes appears inside a function which is provided as a callback. It happens to use Backbone, in case that's relevant. Something like this:
Backbone.View.extend({
// other stuff ...
someFunction: function(param) {
// ...
},
anotherFunction: function() {
this.collection.on("some_event", function() {
this.someFunction.call(this, param);
});
}
});
Does the pattern actually have an effect that isn't the equivalent of this.someFunction(param)
or was someone just nervous about the closure not capturing the correct this
?
Thanks for any insights!
Does the pattern actually have an effect that isn't the equivalent of
this.someFunction(param)
?
No, they are indeed the same. Assuming that this.someFunction
is a function that inherits .call
from Function.prototype
(but that's nit-picking).
Looks like someone was overcautious, or the code is a remains of something that did not use this
twice. Or maybe the author was aware of the this
-context-in-callbacks issue but failed to handle it correctly.
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