5 ms·
Yeah, `call` got supplanted by `bind`, and `bind` got supplanted by arrow functions. `apply` got supplanted by spread parameter syntax. I've used them before f
by Jasper_ 4y ago
Yeah, `call` got supplanted by `bind`, and `bind` got supplanted by arrow functions. `apply` got supplanted by spread parameter syntax.
I've used them before for doing stupid reflection hacks (literally just used them today to build a debug UI where I can make sliders out of objects), but it's not code I would really want to ship in production.
- 29athrowaway 4y agoNo. Bind creates a new function, call and apply do not.
- osrec 4y agoMore specifically, bind creates a new function + context, that you can hold a reference to and reuse, but you cannot do this with call or apply.
- singlow 4y agoIn practice though, many places that used apply/call tend to bind instead.
- 29athrowaway 4y agoapply and call invoke a function with a specified receiver object, and arguments. bind creates a new function that is bound to a specified receiver.
- singlow 4y agoOne of the most common places front-end devs encountered call/apply was when binding functions to event handlers. In those cases an anonymous function was often generated which used apply or call. Bind replaces this dance for many cases. Hence, most cases where devs encountered js call/apply were replaced by bind. There are differences between the two, but they largely didn't matter for most cases.
- 29athrowaway 4y agoUnless you have strict performance requirements, then it does matter.
- agumonkey 4y agoI got to use bind for a class based event handler, using arrows would bind this to the wrong context.. but maybe I'm doing it wrong.
- have_faith 4y agoI've used this syntax which auto-binds this, worked for me class Thing { method1 = () => {} method2 = () => { something.addEventListener('click', this.method1); } }
- agumonkey 4y agoFun trick. A bit tangled though
- jimmont 4y agoFor the benefit of anyone who doesn't have a lot of experience with call, bind and arrow functions ()=>{}. No, these don't supplant one another and are separately useful in various ways. Reading the linked articles shows some of the related situations.