Here's the main difference - do you know the number of arguments beforehand?
1) Use
2) Use
1) Use
apply
if you don't know the number of arguments you
will be passing, (or if they are already in an array or array-like object).2) Use
call
otherwise, since there's no need to wrap the arguments in an array.f.call(thisObject, a, b, c); // Fixed number of arguments f.apply(thisObject, arguments); // Forward arguments var args = []; while (...) { args.push(some_value()); } f.apply(thisObject, args); // Unknown number of arguments
No comments:
Post a Comment