When using Object.keys, only the own enumerable properties are retrieved. However, when using getOwnPropertyNames, only the own properties (even if not enumerable) are retrieved. In both cases, properties inherited from the prototype or any higher prototypes are not included.</p>
<p>If your focus is solely on enumerable properties, check out trincot's answer <a href="https://stackoverflow.com/a/40575562/157247">here</a>.</p>
<p>To retrieve all properties, including ones that are not enumerable, you will need to iterate through the prototype chain:</p>
<p><div>
<div>
<pre class="lang-js"><code>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));