现在这是我对Javascript对象的理解...... var a = {};
Object.defineProperties(a, {
one: {enumerable: true, value: 'one'},
two: {enumerable: false, value: 'two'},
});
a.__proto__ = {three : 'three' };
Object.keys(a); // ["one"]
Object.getOwnPropertyNames(a); // ["one", "two"]
for (var i in a) console.log(i); // one three
所以
Object.keys(a);
返回对象自己的密钥,这些密钥也是可枚举的。
Object.getOwnPropertyNames(a); // ["one", "two"]
返回对象自己的密钥,包括不可枚举的密钥。
for (var i in a)
返回对象及其原型链,只返回可枚举的键
那么如何从原型链中获取不可枚举的密钥?
代码::
var o = {a:1};
Object.defineProperty(o,'b',{configurable:true, enumerable :false, value:2,writable: true });
o.__proto__ = {c:3};
Object.defineProperty(o.__proto__,'d',{configurable:true, enumerable :false, value:4,writable: true });
Object.keys(o); // ["a"] -only self nonenum props.
Object.getOwnPropertyNames(o); // ["a", "b"] -only self including enum props.
for (var i in o) console.log(i) // a c -only enum props including from proto.