William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

JavaScript: call vs. apply

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));

var ary = [
    {user:'user1', pass: 'pass1'}, 
    {user:'user2', pass: 'pass2'}

if($.isFunction(call_apply)) {
  // call(thisObject, arg1, arg2, ...)
  call_apply.call(this, ary[0], ary[1]);

  // apply(thisObject, arrayOfArgs)
  call_apply.apply(this, ary);
else {
 console.log('NO function.');

Will return same result.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: