rwallace
rwallace

Reputation: 33639

Getting all the properties of an object in JavaScript

Does JavaScript have a way to get all the properties of an object, including the built-in ones? for... in skips built-in properties, which is usually what you want, but not in this case. I'm using Node.js if that matters, and it's for debugging purposes so it doesn't have to be elegant, fast or portable.

Upvotes: 0

Views: 130

Answers (2)

openorclose
openorclose

Reputation: 175

Yeah it does, just go up through the prototype and get all properties

function getAllProperties(o) {
    var properties = [];
    while (o) {
        [].push.apply(properties, Object.getOwnPropertyNames(o))
        o = Object.getPrototypeOf(o);
    }
    //remove duplicate properties
    properties = properties.filter(function(value, index) {
        return properties.indexOf(value) == index;
    })
    return properties;
}

Upvotes: 3

Tyler.z.yang
Tyler.z.yang

Reputation: 2450

Well, for debug you could use this:

console.log(yourObject);

Simple and fast. Both in node and in browser. : )

Upvotes: 0

Related Questions