想了解delete的机制缘起一个现象,我无法解释,也无法理解。
var x = 1; delete x; //false
y = 2; delete y; //true
x = 42; // creates the property x on the global object var y = 43; // declares a new variable, y myobj = { h: 4, k: 5 }; delete x; // returns true (x is a property of the global object and can be deleted) delete y; // returns false (delete doesn't affect variable names) delete Math.PI; // returns false (delete doesn't affect certain predefined properties) delete myobj.h; // returns true (user-defined properties can be deleted) delete myobj; // returns true (myobj is a property of the global object, not a variable, so it can be deleted)
特性名 |
说明
|
Configurable
|
设置属性是否可配置,即能否更改(包括名值)或者删除(delect)它,能否修改属性特性等等
|
Enumerable
|
设置属性是否可以枚举,即能否通过for-in循环返回
|
Writable
|
是否可写
|
Value
|
就是属性值,对象在读取属性值时就是从这个位置读取的.
|
var x = 1; Object.getOwnPropertyDescriptor(window, 'x'); //configurable= false delete x; //false y = 2; Object.getOwnPropertyDescriptor(window, 'y'); //configurable= true delete y; //true
function sum() {} sum = 1; Object.getOwnPropertyDescriptor(window, 'sum'); //configurable= false delete sum; //false
Object.defineProperties(window, {sum1:{value:1,configurable:true}}); Object.getOwnPropertyDescriptor(window, 'sum'); //configurable= true delete sum1; //true Object.defineProperties(window, {sum2:{value:1,configurable:false}}); Object.getOwnPropertyDescriptor(window, 'sum'); //configurable= false delete sum2; //false