1

I am using a third party library that provide some callbacks for a widget, but I'm not sure what the callback parameter objects are (no docs on them).

Is there a way to just dump all the attributes of an object in javascript, then print them using alert(), maybe? I just want to see what methods and attributes they contain,

Thanks

user246114
  • 50,223
  • 42
  • 112
  • 149

1 Answers1

4

Well, you can enumerate all object properties using the for...in statement, for example:

if (typeof Object.keys != 'function') {
  Object.keys = function (obj) {
    var result = [];
    for (var prop in obj) {
      if (Object.prototype.hasOwnProperty.call(obj, prop)) {
        result.push(prop);
      }
    }
    return result;
  };
}

alert(Object.keys({foo: 1, bar: 2})); // "foo, bar";

But for debugging purposes I would highly encourage you to get a real debugger, like Firebug.

With the Console API you can easily examine objects on the fly.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • Note that the `for...in` statement will not work on built-in objects like `Math`, although not really relevant to this question it's worth knowing. http://stackoverflow.com/questions/2257993/how-to-display-all-methods-in-a-javascript-object/2258232#2258232 – Andy E Jun 11 '10 at 22:46
  • 1
    @Andy, yeah almost all properties of built in objects are non-enumerable... This approach doesn't deal with another problem also, the infamous [JScript DontEnum Bug](https://developer.mozilla.org/en/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug)... As I said, the best thing is to use a tool like Firebug or the Webkit/Chrome Developer tools... There simply: `console.dir(object);` – Christian C. Salvadó Jun 11 '10 at 22:51
  • @CMS: interesting bug, I didn't know about that! I guess that makes my comment more relevant than I thought. I already gave your answer a +1, so the best I can do is give your comment a +1. I'll also update my answer in that question to make it more complete. – Andy E Jun 11 '10 at 23:00
  • 1
    @Andy, Thanks!, this ugly bug is still present on every IE version <= 8 (it was fixed on IE9 Preview :), seems like a *forgotten bug*, because there are even serious JS libraries that have methods that can malfunction due this... – Christian C. Salvadó Jun 11 '10 at 23:06
  • @CMS: It makes sense that it's fixed in IE9, I read somewhere (might be the blog) that the JScript engine has been completely rewritten. In fact, every time they refer to scripting in IE9 on the blog, they call it Javascript, not JScript, and the JScript blog itself has been silent for almost a year. RIP JScript? :-) – Andy E Jun 11 '10 at 23:13
  • 1
    @Andy hehe, they still need to work more on the engine, a lot of old and perpetual bugs still there, such as the bug on named function expressions :( – Christian C. Salvadó Jun 11 '10 at 23:23
  • @CMS: Might be a good idea to file some bug reports. I'll check it out next week if I get time. It would be nice to have IE9 working as well as possible with Javascript. – Andy E Jun 11 '10 at 23:35