Chrome displays different object contents on expand

时光毁灭记忆、已成空白 提交于 2019-11-26 22:26:36

问题


Why does Chrome display two differing datasets depending on if you have the object view expanded?

In contracted view, my object has two properties:

In expanded view, my object has three properties:


回答1:


The object you see in the console is a snapshot of the object at a particular point in time - the time when you logged it. When you expand the object, it will evaluate the properties again.

In the example below, I have created an object with two array properties. I logged it the console, and then I added a third property, c to it.

Only the first two properties are showing still, even though I just added a third property. After expanding the object in the console, I can see the third one. It is the latest state of the object.

If you hover over the little blue i icon, it explains what it has done:

Value below was evaluated just now.




回答2:


@Gideon Pyzer is right. the properties were caculated and added after expanding the object in the console.

Just add one line code above your debug code and reopen the chrome dev tool, you will see the differences.

obj = Object.freeze(obj);  //add this line before your console.log
console.log(obj);

Before:

After:

one similar question of mine: Why can't I access the attr of the javascript object shown in chrome dev tool




回答3:


You can clone your object to prevent re-evaluate.

console.log(Object.assign({}, obj));



回答4:


You can get a real hard copy object by using this module. It will give you the snapshot of the object at moment when you call it.

https://www.npmjs.com/package/nest-object-deep-copy

const nestedHardCopy = require('nest-object-deep-copy');

console.log(nestedHardCopy(obj));


来源:https://stackoverflow.com/questions/42031634/chrome-displays-different-object-contents-on-expand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!