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

后端 未结 6 2278
小蘑菇
小蘑菇 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 06:00

    Another case for using call() is when you are in an environment where you can't trust the object's own method hasn't been replaced or you know it doesn't actually have the method you want to run on it. hasOwnProperty is often such a method - there are many places where javascript objects may not have their own hasOwnProperty method so invoking it as hasOwnProperty.call(obj, propertyName) is prudent.

提交回复
热议问题