1.查詢和設置:
for in遍歷屬性名;
for (propName in obj) {
console.log(propName);
console.log(obj[propName]);
}
2.刪除屬性:
(1).delete在什麼都沒執行的情況和成功的情況下返回true
o = {x:1};
//成功刪除
delete o.x;
//沒有意義返回true
delete o.x;
//刪除繼承的屬性,沒有執行 true
delete o.toString;
//無意義, true
delete 1;
(2). 在不能刪除的情況下返回false:
//不能刪除不可配置的屬性
delete Object.prototype;
//不能刪除聲明的全局變量(var聲明)和方法
var x = 1;
function f(){};
delete this.x;
delete this.f;
(3).不使用var聲明的全局屬性視爲可配置的可以用delete刪除
//等同this.x = 1
x = 1;
delete x;
3.檢測屬性
(1). in檢測:
var o = {};
"toString" in o; //true
(2). hasOwnProperty:檢測自有屬性
o.hasOwnProperty("toString"); //false
(3). propertyIsEnumerable:檢測自有並且可枚舉的屬性
(4). !==判斷
o.toString !== undefined;