The Object.keys(obj)
returns the own enumerable properties of the object this is analogous to Object.hasOwnProperty()
which are true. What I am looking for is a function that would give all properties even from the prototype chain
Asked
Active
Viewed 194 times
1

Neelotpal
- 337
- 5
- 17
-
2`for in` will loop over every property in the prototype chain. – Jake Holzinger Jul 20 '17 at 06:16
2 Answers
0
Use for ... in
to iterate over all properties of an object:
var myObject = {ownProp: 1};
for (let prop in myObject) {
console.log(prop);
}

Chava Geldzahler
- 3,605
- 1
- 18
- 32

Lars Beck
- 3,446
- 2
- 22
- 23
-
Thanks, I've used it and created `Object.allKeys = function(o){ var arr = []; for (let prop in o) { arr.push(prop); } return arr; }` hope this is correct. – Neelotpal Jul 20 '17 at 06:53
-
0
This question was answered here. There's no built-in method for it.
Bergi's suggestion: (Using .getPrototypeOf()
to traverse up the prototype chain and recursively get the properties of every object)
var myObj = {ownProp1:1, ownProp2:2};
function logAllProperties(obj) {
if (obj == null) return; // recursive approach
console.log(Object.getOwnPropertyNames(obj));
logAllProperties(Object.getPrototypeOf(obj));
}
logAllProperties(myObj);

Chava Geldzahler
- 3,605
- 1
- 18
- 32