Home>

[Scenario 1] This in the global environment points to a global object

this.a=10;
alert (a);//10
b=20;
alert (this.b);//20
var c=30;
alert (this.c);//30

[Scene 2] This of the internal function of the object points to the current object of the calling function

var a=10;
var bar={
 a:20, test:function () {
  alert (this.a);
 }
}
bar.test ();//20

[Scene 3] This of the global environment function points to the global object

var a=10;
function foo () {
 alert (this.a);
}
foo ();//10

[Scenario 4] this in an anonymous function points to a global object

var a=10;
var foo={
 a:20, fn:(function () {
  alert (this.a);
 }) ()
}
foo.fn //10

[Scenario 5] This in the setinterval and settimeout timers points to the global object

var a=10;
var otimer1=setinterval (function () {
 var a=20;
 alert (this.a);//10
 clearinterval (otimer1);
}, 100);

[Scene 6] this in 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 ();

[Scenario 7] This in the constructor points to the constructed new object

function person (name, age) {
 this.name=name;
 this.age=age;
 this.sayname=function () {
  alert (this.name);
 }
}
var p1=new person ("lily", "20");
p1.sayname ();//"lily"

[Scene 8] this in the 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] this in apply and call points to the object in the parameter

var a=10;
var foo={
 a:20, fn:function () {
  alert (this.a);
 }
};
var bar={
 a:30
}
foo.fn.apply ();//10 (if the parameter is empty,(Defaults to global objects)
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
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);
 }
}
a.fn;//2 [this in an anonymous function points to a global object]
a.fn1 ();//3 [this of the object internal function points to the current object that calls the function]
</pre>
</div>
<p>
[Composite scene 3]
</p>
<div>
<pre>
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 changes the pointing of this, making this change from pointing to a global variable to pointing to 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);
 }
};
nameobj.waitshowname ();//"bob" [forms an anonymous function,this points to a global variable]
  • Previous C # method to implement program waiting for delayed execution
  • Next A brief introduction to the thread pool in Java programming