| 遍历自身属性 | 遍历继承属性 | 遍历不可枚举属性 | 遍历 Symbol 类型属性 |
---|
Object.getOwnPropertyNames(obj) | ✔ | ✘ | ✔ | ✘ |
Object.getOwnPropertyDescriptor(obj, prop) | ✔ | ✘ | ✔ | ✔ |
Object.getOwnPropertyDescriptors(obj) | ✔ | ✘ | ✔ | ✔ |
Object.getOwnPropertySymbols(obj) | ✘ | ✘ | ✔ | ✔ |
Object.keys(obj) | ✔ | ✘ | ✘ | ✘ |
Reflect.ownKeys(target) | ✔ | ✘ | ✔ | ✔ |
obj.hasOwnProperty(prop) | ✔ | ✘ | ✔ | ✔ |
for-in | ✔ | ✔ | ✘ | ✘ |
function ParentClass() {
this.foo = 'foo';
}
ParentClass.prototype.inheritedMethod = function() {};
function ChildClass() {
this.bar = 'bar';
this.method = function() {};
}
ChildClass.prototype = new ParentClass;
ChildClass.prototype.prototypeMethod = function() {};
var obj = new ChildClass();
Object.defineProperty(obj, 'baz', {
enumerable: false,
configurable: false,
writable: false,
value: 'baz'
});
var quxSymbol = Symbol();
Object.defineProperty(obj, quxSymbol, {
enumerable: false,
configurable: false,
writable: false,
value: 'qux symbol'
});
Object.keys(obj);
Object.getOwnPropertyNames(obj);
Reflect.ownKeys(obj);
let arr = []; for (const item in obj) { arr.push(item) }