关于highlight()中this的指向问题
function Highlight(){
var tbody = document.getElementById('table').lastChild;
trs = tbody.getElementsByTagName('tr');
for(var i =1;i<trs.length;i++){
trs[i].onmouseover = function(){
this.style.backgroundColor ="#f2f2f2";
}
trs[i].onmouseout = function(){
this.style.backgroundColor ="#fff";
}
}
}this指向最终调用它的对象 上述代码中this指向了trs[i]
但是!!
var bar = function () {console.log(this)}
bar()此处的this则是指向了window
这边感觉两处代码形式基本相同 为何指向不一样呢