本文介绍了如何使console.log显示对象的当前状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有附加组件的Safari(实际上是大多数其他浏览器)中,console.log将在最后执行状态而不是在调用console.log的状态下显示对象.

In Safari with no add-ons (and actually most other browsers), console.log will show the object at the last state of execution, not at the state when console.log was called.

我必须克隆对象只是为了通过console.log将其输出,以获取该行的对象状态.

I have to clone the object just to output it via console.log to get the state of the object at that line.

示例:

var test = {a: true}
console.log(test); // {a: false}
test.a = false;
console.log(test); // {a: false}

推荐答案

我认为您正在寻找console.dir().

console.log()不会执行您想要的操作,因为它会打印对该对象的引用,并且在您将其弹出时将其更改. console.dir在调用对象时在对象中打印属性的目录.

console.log() doesn't do what you want because it prints a reference to the object, and by the time you pop it open, it's changed. console.dir prints a directory of the properties in the object at the time you call it.

下面的JSON想法是一个好主意;您甚至可以继续解析JSON字符串,并获得可浏览的对象,如.dir()会给您的内容:

The JSON idea below is a good one; you could even go on to parse the JSON string and get a browsable object like what .dir() would give you:

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

这篇关于如何使console.log显示对象的当前状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:45