Apply and call allow execution of a function by reference with something explicit passed in as a value for this. If you don't use this then you don't need call or apply. Call, apply, and bind are all new with ES5. I absolutely promise you can program large elegant decomposable applications without these.
No, apply also allows passing varargs into a function. That it also supports passing in an explicit this context isn't important here. My example makes no reference to any this, so I'm confused why you would bring that up. Also, apply and call are not ES5. I'm not sure where that idea comes from. bind - yes. But the other two are ES3. But just out of curiosity - maybe I'm missing something. How would you implement the following ES3 snippet without apply or call?
function traceAndCall(fn) {
console.log("Called function");
fn.apply([].slice.call(arguments, 1));
}
I'm not saying that this is the kind of code you'd have all over the place. I'm just saying that you will be using it one way or another. Hiding it behind a lodash helper doesn't count.
P.S.: That snippet is the pre-ES6 version of the above ES6 code.
It is generally considered bad form to pass arguments of unknown type or quantity into a function. This makes the reference invocation (function arguments are reference declarations) challenging to predict, which interferes with compilation in the JIT. I am sure this may be of great convenience to your code, but I would recommend against it.
0
u/[deleted] Mar 09 '15
Apply and call allow execution of a function by reference with something explicit passed in as a value for this. If you don't use this then you don't need call or apply. Call, apply, and bind are all new with ES5. I absolutely promise you can program large elegant decomposable applications without these.