this执行主体:谁把它执行的
第一种:函数执行,看前面是否有"点",有,“点”前面是谁this就是谁;没有,this是window(严格模式下是undefined),自执行函数中的this一般都是window。
fn() this: window
obj.fn() this: obj
obj.__proto__.fn() this: obj.__proto__
第二种:给元素的事件行为绑定方法(DOM0/DOM2),事件触发,方法会执行,此时方法中的this一般都是当前元素本身
box.onclick = function() {
//=>this:box
}
box.addEventListener('click',function(){
//=>this:box
})
//特殊情况 :IE8以下,基于attachEvent完成DOM2事件绑定,this是不准确的
box.attachEvent('onclick',function(){
//=>this: window
})