console.log对象处于当前状态 [英] console.log object at current state

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

问题描述

console.log 将显示处于最后执行状态的对象,而不是显示 console.log 被调用。



我必须克隆对象,只是通过 console.log



如何更改console.log的默认行为? (


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


解决方案

找到 console.dir()



console.log code>不会做你想要的,因为它打印一个对象的引用,当你弹出它打开,它的改变。 console.dir 在调用对象时打印对象中属性的目录。



JSON构思下面是一个好的;你甚至可以继续解析JSON字符串,并得到一个可浏览的对象,如.dir()会给你:



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


console.log will show the object at the last state of execution, not at the state when console.log was called.

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

How can I change the default behavior of console.log? (Error console in safari, no add-on)

Example:

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

解决方案

I think you're looking for 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.

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对象处于当前状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆