Welcome...



...All those moments will be lost in time, like tears in rain....

- soliloquy from Blade Runner


Bits and bytes do get lost, awash in the rain of data flow that is Internet. They slip away from us, never to found again... some of them will be captured here, many more will not... like tears in rain...



Monday, November 4, 2013

Difference between call and apply in JavaScript

 Here's the main difference - do you know the number of arguments beforehand?

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