web-profile

javascript - difference between call() and apply()

The main difference is that apply() lets you invoke the function with arguments as an array; call() requires the parameters be listed explicitly.

function foo(a, b, c) {}
var bar = {};
foo.apply(bar, [1, 2, 3]); // array will expand to the below
foo.call(bar, 1, 2, 3); // results in a = 1, b = 2, c = 3

Leave a Reply

Your email address will not be published.