This in the Scenario 1 Global environment points to the global object
THIS.A = ten;
alert (a);//10
B =;
alert (this.b);//20
var c =;
alert (THIS.C);//30
This of the internal function of the scene 2 object points to the current object of the calling function
var a = ten;
var bar = {
a:20,
test:function () {
alert (THIS.A);
}
}
Bar.test ();//20
This is the scenario 3 Global environment function point to the global object
var a = ten;
function foo () {
alert (THIS.A);
}
Foo ();//10
This in the "Scene 4" anonymous function points to the global object
var a = ten;
var foo = {
a:20,
fn: (function () {
alert (THIS.A);
}) ()
}
FOO.FN//10
This point in the scene 5 setinterval and settimeout timer points to the global object
var a = ten;
var oTimer1 = setinterval (function () {
var a =;
alert (THIS.A);//10
clearinterval (oTimer1);
},100);
This in the scenario 6 eval points to this in the calling context
(function () {
eval ("alert (This)");//[object window]
}) ();
function Foo () {
This.bar = function () {
eval ("alert (This)");//[object object]
}
var foo = new Foo ();
Foo.bar ();
This in the scenario 7 constructor points to a new object that is constructed
function Person (name,age) {
this.name = name;
This.age = age;
This.sayname = function () {
alert (this.name);
}
}
var p1 = new Person (' Lily ', ' m ');
P1.sayname ()//' Lily '
This in the "Scene 8" new function points to the global object
(function () {
var f = new Function ("alert (This)");
f ();//[object Window]
}) ();
function Foo () {
This.bar = function () {
var f = new Function ("alert (This)");
f ();//[object window]
}
var foo = new Foo ();
Foo.bar ();
"Scene 9" in Apply and call, this points to an object in a parameter
var a = ten;
var foo = {
a:20,
fn:function () {
alert (THIS.A);
}
};
var bar ={
a:30
}
foo.fn.apply ();//10 (if the argument is null, the default points to global object)
foo.fn.apply (foo);//20
foo.fn.apply (bar);//30
"Composite Scene 1"
var someone = {
name: "Bob",
showname:function () {
alert (this.name);
}
;
var other = {
name: "Tom",
showName:someone.showName
}
other.showname (); Tom
/The above function
is equivalent to var other = {
name: "Tom",
showname:function () {
alert (this.name);
}
}
Other.showname (); Tom
"Composite Scene 2"
var name = 2;
var a = {
Name:3,
fn: (function () {
alert (this.name);
}) (),
fn1:function () {
alert (this.name);
}
}
The This in the A.fn;//2[anonymous function points to the global object]
a.fn1 (); this point to the//3[object's internal function points to the current object of the calling function.
"Composite Scene 3"
var name = "Bob";
var nameobj ={
Name: "Tom",
showname:function () {
alert (this.name);
},
waitshowname:function () {
var that = this;
settimeout (function () {
that.showname ();
}, 1000);
}
;
Nameobj.waitshowname ();//"Tom" [That=this to change the point of this, so that this changes from point to global variable to point nameobj]
var name = "Bob";
var nameobj ={
Name: "Tom",
showname:function () {
alert (this.name);
},
waitshowname: function () {
var that = This;//that points to nameobj
settimeout (function () {
(function () {
alert ( this.name);
}) ();
}, 1000);
}
;
This article has introduced this 9 kinds of application scene, hoped that has the help to everybody, this station has the new content renewal every day, requests the broad netizen to continue to pay attention to this station, thanks.