对象转储 JavaScript

是否有第三方插件/应用程序或其他方法在脚本调试器中为 JavaScript 对象执行对象映射转储?

情况是这样的... ... 我有一个方法被调用了两次,每次都有不同的地方。我不知道有什么不同,但肯定有什么不同。因此,如果我可以将 window (或者至少 window.document)的所有属性转储到一个文本编辑器中,我就可以用一个简单的文件 diff 比较两个调用之间的状态。有什么想法吗?

167799 次浏览

Firebug + console.log(myObjectInstance)

function mydump(arr,level) {
var dumped_text = "";
if(!level) level = 0;


var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";


if(typeof(arr) == 'object') {
for(var item in arr) {
var value = arr[item];


if(typeof(value) == 'object') {
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += mydump(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
}
}
} else {
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}

For Chrome/Chromium

console.log(myObj)

or it's equivalent

console.debug(myObj)
console.log("my object: %o", myObj)

Otherwise you'll end up with a string representation sometimes displaying:

[object Object]

or some such.

Just use:

console.dir(object);

you will get a nice clickable object representation. Works in Chrome and Firefox

If you're on Chrome, Firefox or IE10 + why not extend the console and use

(function() {
console.dump = function(object) {
if (window.JSON && window.JSON.stringify)
console.log(JSON.stringify(object));
else
console.log(object);
};
})();

for a concise, cross-browser solution.

In Chrome, click the 3 dots and click More tools and click developer. On the console, type console.dir(yourObject).Click this link to view an example image

Using console.log(object) will throw your object to the Javascript console, but that's not always what you want. Using JSON.stringify(object) will return most stuff to be stored in a variable, for example to send it to a textarea input and submit the content back to the server.

for better readability you can convert the object to a json string as below:

console.log(obj, JSON.stringify(obj));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify