The value is thisset implicitly depending on how the function is called , there are three cases when this happens:
If the link with the base object or link is not referenced:
myFn();
(function () {})();
Value thiswill point to global object 1
If the link contains a base object, for example:
myObj.method();
The value thisinside methodwill point to myObj.
When a statement is used new:
var obj = new Foo();
this Foo , Foo.prototype.
this , call apply, , call:
function test(a) {
return alert(this + a);
}
test.call("hello", " world");
apply, "" :
function test(a, b) {
return alert(this + a + b);
}
var args = ["my ", "world "];
test.apply("hello ", args);
[1] ECMAScript 5th Strict Mode, , ( ), this undefined.
, new .
, this , .
this undefined, (this.foo = 'foo'), TypeError Foo.