每日一题(四四)document.querySelector(button).onclick = function(){ console.log(this)}

 this 指向谁

//页面中有一个 button 按钮
document.querySelector('button').onclick = function(){
    console.log(this);
}

答案:

不确定

解析:

考察发散思维能力,不是 window 不是 document 也不是 button ,因为题目中没有告诉你谁去触发点击事件,所以就不知道 this 指向谁,比如:

document.querySelector('button').onclick = function() {
   console.log(this)
}

let fn = document.querySelector('button').onclick;

fn(); // fn 指向 window

let a = {fn}

a.fn(); //指向a

猜你喜欢

转载自blog.csdn.net/MFWSCQ/article/details/106288909