简体   繁体   中英

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.

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

Well, for debug you could use this:

console.log(yourObject);

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

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