Dump methods and attributes of object
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.
精彩评论