- 【情况1:普通函数内部的this指向window】
- 【情况2:定时器内部的this指向window】
- 【情况3:在构造函数中,this指向当前创建的对象】
- 【情况4:对象的方法中,this指向调用者】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
function fn() {
console.log(this);
}
fn();
(function () {
console.log(this);
})();
(function () {
function fn() {
console.log(this);
}
fn();
})();
setTimeout(function () {
console.log(this);
}, 10);
function Person(name, age) {
this.name = name;
this.age = age;
console.log(this);
}
var p1 = new Person('zs', 10);
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayHi = function () {
console.log(this);
};
var p1 = new Person('zs', 10);
var p2 = new Person('ls', 20);
document.onclick = function () {
console.log(this);
};
console.dir(document);
</script>
</body>
</html>
```javascript