透過var宣告的變數delete不能刪除,但是它也是window的屬性啊?求解釋
var foo = 1;
window.bar = 2;
delete foo;
delete bar;
console.log(window.foo,window.bar)//1 undefined
过去多啦不再A梦2017-06-14 10:54:39
因為用var
宣告的變量,其屬性是configurable = false
的,所以不能刪除。
var ss = 0;
console.log(Object.getOwnPropertyDescriptor(window, 'ss'));
/*
{
configurable: false
enumerable: true
value: 0
writable: true
}
*/