this
this 的指向在函数定义的时候 是确定不了的 只有函数执行的时候才能确定this指向谁 一般情况下 this 的最终指向的是那个调用它的对象
- .全部作用域或者普通作用函数中 this指向全局对象window (定时器里面的 this指向window)
- 方法调用中 谁调用this指向谁
- 构造函数this指向构造函数
<body>
<button> 按钮</button>
<script>
// this 指向问题 一般情况下 this最终指向是调用它的对象
// 1.全部作用域或者普通作用函数中 this指向全局对象window (定时器里面的 this指向window)
console.log(this);
function fn (){
console.log(this);
}
fn()
// 定时器 指向的也是window
// setInterval(function(){
// console.log(this);
// },1000)
// 2.方法调用中 谁调用this指向谁
var q = {
hello:function(){
console.log(this); //this 指向的是 q 这个对象this最终指向是调用它的对象
}
}
q.hello()
var btn = document.querySelector('button')
btn.onclick = function(){
console.log