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

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

    Using a function's call() method allows you to changed the object that's bound to the function as this during the execution of the function - this is also called the context

    their_on_success.call(myContext, results)
    

    But, if your callback function does not depend on this, it make no difference which way you call it.

提交回复
热议问题