如何在Safari中更改console.log的默认行为? [英] How can I change the default behavior of console.log in Safari?

查看:485
本文介绍了如何在Safari中更改console.log的默认行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有附加组件的Safari中, console.log 将显示最后执行状态的对象,而不是控制台时的状态.log 被调用。

In Safari with no add-ons, 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)));

这篇关于如何在Safari中更改console.log的默认行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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