开发者

Dump methods and attributes of object

开发者 https://www.devze.com 2023-01-03 17:40 出处:网络
I am using a third party library that provide some ca开发者_Python百科llbacks for a widget, but I\'m not sure what the callback parameter objects are (no docs on them).

I am using a third party library that provide some ca开发者_Python百科llbacks 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消