When should I use call() vs invoking the function directly?

后端 未结 6 2299
小蘑菇
小蘑菇 2021-02-19 05:14

I\'ve got a JavaScript application that uses a lot of callbacks. A typical function will take a callback, and wrap it with another callback.

Namespace.foo = func         


        
6条回答
  •  感情败类
    2021-02-19 05:37

    One scenario I often use "call" is splice arguments:

    const fn = function() {
        const args = Array.prototype.slice.call(arguments, 0);
        console.log(args);
    }
    

    Notice that arguments is an object. the args however is an array.

提交回复
热议问题