這個問題的Can't understand the behavior of deleting vars in JavaScript爲什麼hasOwnProperty返回true
這是發生在谷歌瀏覽器的擴展: - 案例1:
var x = 5;
window.x === x // true. x, as it seems, is a property of window
window.hasOwnProperty('x'); // true
delete x; // false
delete window.x; // false;
案例2:
window.x = 5;
delete window.x; // true
案例3:
window.x = 5;
delete x; // true
如果CASE 2和3有效(刪除x),那麼CASE1爲什麼不這樣做?我知道刪除只會刪除任何屬性,並且在CASE 1中x是一個不應該是屬性的變量。但是window.hasOwnProperty函數返回true。
在Firefox爲CASE1:hasOwnProperty返回true,並且還刪除X返回true ...
只有當'x'被聲明爲'x = 5;',沒有'var'時,第一種情況纔是'true'。 – thefourtheye
無法使用FF 26.0重現,FF在所有實例上返回true。 – Zeta
但是當我們聲明它爲var x,那麼它也是窗口對象的屬性..所以它應該刪除 –