JavaScript: call vs. apply
Blogs20132013-06-23
JavaScript: call vs. apply
JavaScript call() requires the parameters be listed explicitly, when apply() accepts an array as parameter, here is my usage:
function call_apply(x,y) {
console.log(JSON.stringify(x) +','+JSON.stringify(y));
}
if($.isFunction(call_apply)) {
console.log('function:');
// call(thisObject, arg1, arg2, ...)
call_apply.call(this,
{user:'user1', pass: 'pass1'},
{user:'user2', pass: 'pass2'}
);
// apply(thisObject, arrayOfArgs)
call_apply.apply(this, [
{user:'user1', pass: 'pass1'},
{user:'user2', pass: 'pass2'}
]);
}
else {
console.log('NO function.');
}Will return same result.
