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

后端 未结 6 2274
小蘑菇
小蘑菇 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:41

    call() is used to change the this value of the function:

    var obj = {a: 0};
    method.call(obj, "parameter");
    function method(para) {
        this.a == 0; // true <-- obj is now this
    }
    

提交回复
热议问题