What is the difference between call and apply?

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

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

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


        
24条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 08:05

    The call() method calls a function with a given this value and arguments provided individually.

    apply() - Similar to the call() method, the first parameter in the apply() method sets the this value which is the object upon which the function is invoked. In this case, it's the obj object above. The only difference between the apply() and call() method is that the second parameter of the apply() method accepts the arguments to the actual function as an array.

提交回复
热议问题