属性操作
getOwnPropertyDescriptor: 获取属性中的标签
var descriptor = Object. getOwnPropertyDescriptor(Object, 'prototype');
descriptor.configurable; // false
属性监测
var cat = new Object;
cat.legs = 4;
cat.name = 'kitty';
'legs' in cat; // true
'abc' in cat; // false
'toString' in cat; // true, inherited prototype!!!
cat.hasOwnPrototype('legs'); // true
cat.propertyIsEnumerable('legs'); // true,是否可枚举
cat.propertyIsEnumerable('toString'); // false
字面量或 new Object 创建的对象,默认 可枚举,可写,可 delete
defineProperty 默认全是 false
Object.defineProperty(cat, 'price', {enumerable: false, value: 1000});
cat.propertyIsEnumerable('price'); // false
cat.hasOwnProperty('price'); // ture
if(cat && cat.legs) {
cat.legs *= 2;
}
//不写严格等于 null == undefined
if(cat.legs != undefined) {
// !== undefined, or, !== null
}
if(cat.legs ! == undefined) {
// only if cat.legs is not undefined
}
属性枚举
var o = {x: 1, y: 2, z: 3};
'toString' in o; // true
o.propertyIsEnumerable('toString'); // false
var key;
for(key in o) {
console.log(key); // x, y, z
}
var obj = Object.create(o);
obj.a = 4;
var key;
for(key in obj) {
console.log(key); // a, x, y, z
}
var obj = Object.create(o);
obj.a = 4;
var key;
for(key in obj) {
if(obj.hasOwnProperty(key)) {
console.log(key); //a
}
}