1. The call method always expect comma separated parameters.
2. The apply method always expect array of arguments.
3. However, call and apply both expecting object as first parameter.
Note:
In ES6, we know spread operator ... and you can pass an array (or any iterable) as a list of arguments.
So using this spread with call, almost you are achieving apply concept.
1 2 3 4 | let args = [1, 2, 3]; func.call(context, ...args); // pass an array as list with spread operator func.apply(context, args); |
From the above code you can find some minor difference, let see what is that
1. The spread operator ... is iterable args.
2. The apply accepts only array-like args.
So an iterate is possible with call and it works. Whereas we expect an array-like, apply works.
Generally apply will be faster because of its single operation.
No comments:
Post a Comment