当前位置: 首页 - 编程技术 - 文章正文

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

xiaoqihv

 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

 

 

文章地址:https://wenmayi.cn/post/708.html