1

I am learning javascript via javascript.info. I learned about bind (and a lesson before about call and apply) I see this post inquires about the differences on all 3.

After reading all that, I wonder: is there a scenario where using bind would NOT cover the need for using either call or apply ? I mean, call or apply are meant for calling the function immediately. bind - later on.

Would not the following work in all cases?

let f = function(){/* code here */};
let errorFreeF = f.bind(myContextObj);
errorFreeF(); 

A less verbose snippet:

function(){/* code here */}.bind(myContextObj)();

If the answer is using bind is always safe and indeed covers all scenarios, seems like the call/apply could be deprecated in future JS versions ? Or it performs worse?

Veverke
  • 4,668
  • 1
  • 37
  • 78

1 Answers1

2

You can use .bind as a substitute for .call and .apply. If you wanted, you could convert all uses of .call and .apply with .bind.

The other combinations are true as well. If you really wanted to, you could also replace all instances of those methods with .call, or with .apply.

It's just that it's sometimes inelegant to do so. For similar reasons, even though Array.prototype.forEach could be used to identify an element which matches a condition in an array, Array.prototype.find is more appropriate and requires less code.

seems like the call/apply could be deprecated in future JS versions ?

No, for a few reasons:

  • They're useful methods to have (they can result in cleaner code than having to write the same thing with .bind)
  • Using .bind creates a function. In rare cases, the extra overhead resulting from the creation of a function may be a problem. (But .call and .apply only invoke functions, they don't create functions)
  • The language designers will never implement a change (such as removing .call or .apply) because that will break existing websites, which is avoided at all costs.
CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
  • Not just less code, but my gut feeling also says using `call` and `apply` would have much better performance in a tight loop etc than repeatedly re-binding to different contexts – James Thorpe Apr 23 '20 at 10:22
  • Yeah, probably, Though, performance is almost never a concern in 99% of code blocks one is writing/analyzing. – CertainPerformance Apr 23 '20 at 10:24
  • Yeah - though I've just spent all morning with my head in performance sensitive code, so that's where it's at at the moment I guess! – James Thorpe Apr 23 '20 at 10:25
  • Should not your "No" be "Yes" :) (I ask if using calling the binded function on the spot would not always do the job - seems you say yes, it would). Regarding more elegant/less verbose code, of course my sample should be `function(){}.bind(myContext)()`; Using bind would still be more verbose but the underlying question is: if bind is enough for every scenario, why not deprecate the other ones ? Maybe its performance is worse then the others... – Veverke Apr 23 '20 at 10:26