this指向问题
1.全局作用域或者普通函数中this指向全局对象window(注意:定时器中的this也指向window)
2.方法调用中谁调用this指向谁
3.构造函数中this指向其实例对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button></button>
<script>
console.log(this)
function fn() {
console.log(this)
}
window.fn()
window.setTimeout(function () {
console.log(this)
},1000)
var o = {
sayHi : function () {
console.log(this)
}
}
o.sayHi()
const btn = document.querySelector('button');
btn.onclick = function () {
console.log(this)
}
btn.addEventListener('click',function () {
console.log(this)
})
function Fun() {
console.log(this)
}
const fun = new Fun()
</script>
</body>
</html>