What is the difference between call and apply?

前端 未结 24 1655
太阳男子
太阳男子 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:06

    We can differentiate call and apply methods as below

    CALL : A function with argument provide individually. If you know the arguments to be passed or there are no argument to pass you can use call.

    APPLY : Call a function with argument provided as an array. You can use apply if you don't know how many argument are going to pass to the function.

    There is a advantage of using apply over call, we don't need to change the number of argument only we can change a array that is passed.

    There is not big difference in performance. But we can say call is bit faster as compare to apply because an array need to evaluate in apply method.

提交回复
热议问题