//只要是宣告過後的變數都不能刪除! 如:var、let、const、function,什麼意思呢? 舉例如下:
var thisVar = "I'm Var";
delete thisVar;
let thisLet = "I'm Let";
delete thisLet;
const thisConst = "I'm Const";
delete thisConst;
function thisFunction(){
console.log("I'm Funciton");
}
delete thisFunction;
//但是如果沒有宣告就可以刪除,舉例如下:
thisUndeclaredVar = "I'm UndeclaredVar";
delete thisUndeclaredVar;
thisUndeclaredFunction = function(){
console.log("I'm UndeclaredFunciton");
}
delete thisUndeclaredFunction;
//雖然物件本身不能刪除,但裡頭的屬性(變數)可以刪除,舉例如下:
var obj = {
a: 'a',
b: 'b',
c: 'c'
}
delete obj;
delete obj.a;
//那你妳可能會想問,我到底該如何知道所有的東西哪些能刪哪些不能刪呢?
//來吧,來下一個關鍵Object.getOwnPropertyDescriptor(物件,'屬性');的指令妳就明瞭了!
Object.getOwnPropertyDescriptor(window,'obj');
Object.getOwnPropertyDescriptor(window.obj,'a');
//更好的方法是使用Object.defineProperty來宣告configurable是否可以被刪除。且在嚴格模式(use stirct)下,不宣告的變數會出錯,例如:
'use strict';
thisUndeclaredVar = "I'm UndeclaredVar";
Uncaught ReferenceError: thisUndeclaredVar is not defined
//所以使用Object.defineProperty來宣告configurable是否可以被刪除,在嚴格模式下才不會有錯誤。
'use strict';
Object.defineProperty(window, 'thisUndeclaredVar', {
value: "I'm UndeclaredVar",
configurable: true
})
delete thisUndeclaredVar; // return true
PS. Object.defineProperty 裡有四種屬性可以設定。
configurable: true or false
enumerable: true or false
value: Number or "string" or Function or Boolean or Object...etc.
writable: true or false
1.configurable
該屬性是否可刪除。true代表可以delete,false則不行。
2.enumerable
該屬性是否可列舉。一樣true代表可列舉,false則不行。通常使用for...in及Object.keys()的情況下,可能會意外的多列印出prototype自己所設定的屬性,所以使用Object.defineProperty來設定在prototype自訂屬性裡的enumerable為false,就不會列印出來了。
3.value
這就不用多講了,跟一般宣告變數給值一樣。
4.writable
該屬性值是否可以覆寫。true代表可以覆寫,false則不行。