<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button id="btn">按钮</button>
<script>
function fn() {
console.log(this); //this指向的是window
}
fn();
/
function Person(age, sex) {
this.age = 22;
this.sex = "男";
console.log(this);
}
var p1 = new Person(18, "男"); //this指向的是person的实例对象p1
/
var obj = {
name: function () {
console.log(this); //this指向的是该方法所属的对象
},
};
obj.name();
/
var obtn = document.getElementById("btn");
obtn.onclick = function () {
console.log(this); //this 指向的是绑定事件的对象 <button id="btn">按钮</button>
};
/
setInterval(function () {console.log(this);}, 1000); //this指向的是window
setTimeout(function () {console.log(this);}, 1000); //this指向的是window
</script>
</body>
</html>