JS shorthand - this

The binding of this has nothing to do with the position of the function declaration (this is the biggest difference from the lexical scope). It depends on how the function is called. The different calling methods of the function determine the bound object of this in the function.

There are 4 different ways to call a function:

function foo(){
    //...
}
foo()//1
var o = Object.create(null);
o.foo = foo;
o.foo();//2
foo.call(window)//3,或者foo.apply(window)
new foo()//4

 

Priority: new > explicit > implicit > default.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325147993&siteId=291194637
Recommended