1

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

Neelotpal
  • 337
  • 5
  • 17

2 Answers2

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
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