1.当有定时器时 this会指向window  

<script type="text/javascript">
		function Aaa(){
			var _this=this;  //_this表示Aaa
			this.a=12;
			
			setInterval(function(){
//				console.log(this)  //this表示window
				_this.show();
			},1000)
		}
		Aaa.prototype.show=function(){
			console.log(this.a)
		}
		var obj=new Aaa();
//		obj.show()
	</script>

2.当有事件时,this会指向事件对象

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<title>Document</title>
</head>
<body>
	
	<script type="text/javascript">
		function Bbb(){
//			console.log(this)   //this 这个表示 Bbb
			var _this=this;
			this.b=5;
//			document.getElementById("btn").onclick=this.show  //这里的this表示 input
			document.getElementById("btn").onclick=function(){
				_this.show()
			}
		}
		Bbb.prototype.show=function(){
			console.log(this.b)  /*this 表示*/
		}
		window.onload=function(){
			  new Bbb();
		}
	
	</script>
	
	<input type="button" name=""  />
</body>
</html>

  

相关文章:

  • 2021-08-20
  • 2021-11-06
  • 2021-06-21
  • 2022-01-03
  • 2021-05-17
  • 2021-04-14
  • 2021-07-22
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-03
  • 2022-01-12
  • 2022-12-23
  • 2021-07-14
相关资源
相似解决方案