简体   繁体   中英

Javascript - List all properties/methods from an object

I know that an object literal can have its keys listed by using Object.keys() method. But what about an object that was created through a constructor function?

function Foo () {}
Foo.prototype.bar = 'bar';
Foo.prototype.baz = 'baz';

var foo = new Foo();
console.log(Object.keys(foo).join(', ')); // returns ''
console.log(Object.getOwnPropertyNames(foo).join(', ')); // returns ''

Object.keys will only get own enumerable properties, and getOwnPropertyNames will only get own properties (even if not enumerable). Neither of them will give you the names of properties inherited from the prototype (or its prototype, or its , ...).

If you only care about enumerable properties, see trincot's answer .

If you want all of them,¹ even if they're not enumerable, you have to loop through the prototype chain:

 function getAllPropertyNames(obj) { var result = []; while (obj && obj !== Object.prototype) { result.push.apply(result, Object.getOwnPropertyNames(obj)); obj = Object.getPrototypeOf(obj); } return result; } function Foo () {} Foo.prototype.bar = 'bar'; Foo.prototype.baz = 'baz'; var foo = new Foo(); console.log(getAllPropertyNames(foo)); 

In that example, I stopped when we reached Object.prototype , but of course you could keep going until you hit null instead:

 function getAllPropertyNames(obj) { var result = []; while (obj) { result.push.apply(result, Object.getOwnPropertyNames(obj)); obj = Object.getPrototypeOf(obj); } return result; } function Foo () {} Foo.prototype.bar = 'bar'; Foo.prototype.baz = 'baz'; var foo = new Foo(); console.log(getAllPropertyNames(foo)); 


¹ "If you want all of them..." Note that in the above, we haven't tried to get properties that are named by Symbols instead of strings. If we did, we'd use getOwnPropertySymbols as well as getOwnPropertyNames .

You can use a for loop:

 function Foo () {} Foo.prototype.bar = 'bar'; Foo.prototype.baz = 'baz'; var foo = new Foo(); for (var key in foo) console.log(key, foo[key]); 

Note that this has limitations. Some properties can be made non-enumerable, and will then not be included. This is for instance the case for the length property of arrays:

 var a = [1, 2]; for (var key in a) console.log(key, a[key]); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM