Judging this in JS points to the problem summary

We can determine which rule is applied to a function at a certain call location based on the priority, which can be judged in the following order:

1. Is the function called in new (new binding)? If yes, this is bound to the newly created object .

var bar = new foo();

2. Is the function called by call, apply (display binding) or hard binding? If so, this is bound to the specified object .

var bar = foo.call(obj2);

3. Is the function called in a certain context object (implicit binding)? If so, this context object is bound to this .

var bar = obj1.foo();

4. If neither is the case, use the default binding, if in strict mode, bind to undefined, otherwise bind to the global object .

var bar = foo();

 

Excerpt from "Javascript You Don't Know" (Volume 1)

Guess you like

Origin blog.csdn.net/RedaTao/article/details/108145988