在控制台中运行此代码会产生错误:Cannot read property 'status' of undefined
。
xhr = new XMLHttpRequest(); // global variable
xhr.open('GET', '/');
xhr.onload = function(){
console.log(xhr.status); // this line throws the error
var xhr = 'a'; // local variable
}
xhr.send();
如果我将var xhr = 'a';
行注释掉,它将起作用。有人可以解释这里发生的事情吗?
在Chrome和Firefox中进行了测试。