What is the difference between call and apply?

前端 未结 24 2162
太阳男子
太阳男子 2020-11-21 07:12

What is the difference between using call and apply to invoke a function?

var func = function() {
  alert(\'hello!\');
};
         


        
24条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 08:05

    Call() takes comma-separated arguments, ex:

    .call(scope, arg1, arg2, arg3)

    and apply() takes an array of arguments, ex:

    .apply(scope, [arg1, arg2, arg3])

    here are few more usage examples: http://blog.i-evaluation.com/2012/08/15/javascript-call-and-apply/

提交回复
热议问题