4 ms·
How often do you use call and apply? I've been a JS developer for a long time, have written a lot of JS code, and rarely need to use call and apply. Unless you
by driverdan 12y ago
How often do you use call and apply? I've been a JS developer for a long time, have written a lot of JS code, and rarely need to use call and apply.
Unless you're writing all your code from scratch and not using any frameworks / util libs (which you should) you won't need them very often.
- cozuya 12y agoFairly often. Since the selected DOM element is the value of this in any event handler callback, you can migrate out the function and use it for different things (i.e. validation of, say, autofill after a settimeout) and different contexts with call/apply.
- tericho 12y agoI don't see how this requires you to know the intricacies of call/apply or how it would even force you to use it frequently enough to understand the difference.
- cozuya 12y agoI guess I don't understand. There's a lot of ways to accomplish tasks in any programming language but in JS in particular there's a way to assign the value of this and you can use it to have clean, dry code, and I'd expect any sr. level people to know how to use it. jQuery source has over 100 uses of call and apply.
- driverdan 12y ago> jQuery source has over 100 uses of call and apply. Right, jQuery does it for you. You usually don't need to do it yourself unless you're contributing to jQuery (or another lib/framework that needs them). I might ask someone about call and apply but wouldn't be overly concerned if they didn't remember the exact syntax or mixed them up. I've done the same myself. I wouldn't expect someone to remember every little nuanced part of a language, especially if it's not something they use frequently.
- nogridbag 12y agoAgreed I can't even recall the last time I used call or apply. We might have only a handful of cases out of roughly 200,000 JS lines of code.
- dustingetz 12y agoPerhaps that is why y'all have 200k LOC of javascript :)
- Touche 12y agoIf you do meta-programming in JavaScript you use call/apply all of the time.