What is the difference between call and apply?

前端 未结 24 1742
太阳男子
太阳男子 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 07:50

    While this is an old topic, I just wanted to point out that .call is slightly faster than .apply. I can't tell you exactly why.

    See jsPerf, http://jsperf.com/test-call-vs-apply/3


    [UPDATE!]

    Douglas Crockford mentions briefly the difference between the two, which may help explain the performance difference... http://youtu.be/ya4UHuXNygM?t=15m52s

    Apply takes an array of arguments, while Call takes zero or more individual parameters! Ah hah!

    .apply(this, [...])

    .call(this, param1, param2, param3, param4...)

提交回复
热议问题