What is the difference between call and apply?

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

    The main difference is, using call, we can change the scope and pass arguments as normal, but apply lets you call it using arguments as an Array (pass them as an array). But in terms of what they to do in your code, they are pretty similar.

    While the syntax of this function is almost identical to that of apply(), the fundamental difference is that call() accepts an argument list, while apply() accepts a single array of arguments.

    So as you see, there is not a big difference, but still, there are cases we prefer using call() or apply(). For example, look at the code below, which finding the smallest and largest number in an array from MDN, using the apply method:

    // min/max number in an array
    var numbers = [5, 6, 2, 3, 7];
    
    // using Math.min/Math.max apply
    var max = Math.max.apply(null, numbers); 
    // This about equal to Math.max(numbers[0], ...)
    // or Math.max(5, 6, ...)
    
    var min = Math.min.apply(null, numbers)
    

    So the main difference is just the way we passing the arguments:

    Call:

    function.call(thisArg, arg1, arg2, ...);
    

    Apply:

    function.apply(thisArg, [argsArray]);
    
    0 讨论(0)
  • 2020-11-21 08:01

    Both call and apply the same way. It calls immediately when we use call and apply.

    Both call and apply takes "this" parameter as the first argument and the second argument only differs.

    the call takes the arguments of the functions as a list (comma ) Apply takes the arguments of the functions as an array.

    You can find the complete difference between bind, call, and apply in the bellow youtube video.

    https://www.youtube.com/watch?v=G-EfxnG0DtY&t=180s

    0 讨论(0)
  • 2020-11-21 08:03

    Call and apply both are used to force the this value when a function is executed. The only difference is that call takes n+1 arguments where 1 is this and 'n' arguments. apply takes only two arguments, one is this the other is argument array.

    The advantage I see in apply over call is that we can easily delegate a function call to other function without much effort;

    function sayHello() {
      console.log(this, arguments);
    }
    
    function hello() {
      sayHello.apply(this, arguments);
    }
    
    var obj = {name: 'my name'}
    hello.call(obj, 'some', 'arguments');
    

    Observe how easily we delegated hello to sayHello using apply, but with call this is very difficult to achieve.

    0 讨论(0)
  • 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/

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题