SoFunction
Updated on 2025-03-06

9 application scenarios and three composite application scenarios of this in JavaScript

【Scenario 1】This in the global environment points to the global object

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

【Scene 2】This in the object internal function points to the current object that calls the function

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

【Scenario 3】 This of the global environment function points to the global object

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

【Scene 4】This in anonymous function points to the global object

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

【Scene 5】This in setInterval and setTimeout timers points to the global object

var a = 10;
var oTimer1 = setInterval(function(){
 var a = 20;
 alert();//10
 clearInterval(oTimer1);
},100);

【Scene 6】This in eval points to this in the call context

(function(){
 eval("alert(this)");//[object Window]
})();
function Foo(){
  = function(){
  eval("alert(this)");//[object Object]
 }
}
var foo = new Foo();
();

【Scene 7】This in the constructor points to the constructed new object

function Person(name,age){
  = name;
  = age;
  = function(){
  alert();
 }
}
var p1 = new Person('lily','20');
();//'lily'

【Scene 8】This in new function points to the global object

(function(){
 var f = new Function("alert(this)");
 f();//[object Window]
})();
function Foo(){
  = function(){
  var f = new Function("alert(this)");
  f();//[object Window]
 }
}
var foo = new Foo();
();

【Scene 9】 This in apply and call points to the object in the parameter

var a = 10;
var foo = {
 a: 20,
 fn: function(){
  alert();
 }
};
var bar ={
 a: 30
}
();//10 (If the parameter is empty, the default point to the global object)(foo);//20
(bar);//30

【Compound Scene 1】

var someone = {
 name: "Bob",
 showName: function(){
  alert();
 }
};
var other = {
 name: "Tom",
 showName: 
}
();//Tom

//The above functions are equivalent to
var other = {
 name: "Tom",
 showName: function(){
  alert();
 }
}
();//Tom

【Composite Scene 2】

var name = 2;
var a = {
 name: 3,
 fn: (function(){
  alert();
 })(),
 fn1:function(){
  alert();
 }
}
;//2 [this in anonymous function points to the global object]a.fn1();//3[The internal function of the objectthisPoint to the current object that calls the function]

【Compound Scene 3】

var name = "Bob"; 
var nameObj ={ 
 name : "Tom", 
 showName : function(){ 
 alert(); 
}, 
 waitShowName : function(){
  var that = this;
  setTimeout(function(){
   ();
  }, 1000);
 }
}; 
();//"Tom"[that=this changes the pointing of this, causing this to change from pointing to global variable to pointing to nameObj]
var name = "Bob"; 
var nameObj ={ 
 name : "Tom", 
 showName : function(){ 
  alert(); 
 }, 
 waitShowName : function(){
  var that = this;//that points to nameObj  setTimeout(function(){
   (function(){ 
    alert();
   })();
  }, 1000);
 }
}; 
();// 'Bob'[Form anonymous function,thisPoint to global variables] 

This article introduces 9 application scenarios in this, and hope it will be helpful to everyone. This site has new content updated every day. Please continue to pay attention to this site, thank you.