How to find hidden properties/methods in Javascript objects?

Posted by ramanujan on Stack Overflow See other posts from Stack Overflow or by ramanujan
Published on 2010-05-30T21:32:30Z Indexed on 2010/05/30 21:52 UTC
Read the original article Hit count: 172

I would like to automatically determine all of the properties (including the hidden ones) in a given Javascript object, via a generalization of this function:

function keys(obj) {
    var ll = [];
    for(var pp in obj) {
        ll.push(pp);
    }
    return ll;
}

This works for user defined objects but fails for many builtins:

repl> keys({"a":10,"b":2});  // ["a","b"]
repl> keys(Math) // returns nothing!

Basically, I'd like to write equivalents of Python's dir() and help(), which are really useful in exploring new objects.

My understanding is that only the builtin objects have hidden properties (user code evidently can't set the "enumerable" property till HTML5), so one possibility is to simply hardcode the properties of Math, String, etc. into a dir() equivalent (using the lists such as those here). But is there a better way?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about python