【问题标题】:What differs between console.log and console.dir? [duplicate]console.log 和 console.dir 有什么区别? [复制]
【发布时间】:2012-12-21 10:32:59
【问题描述】:

可能重复:
What’s the difference between console.dir and console.log?

我最近了解到console.dir() 的存在。

看了MDN之后,我并没有很清楚这和console.log的真正区别是什么。它们都显示相同的输出(但.dir 显示了一些属性),是这样吗?

调试/开发时应该使用哪个函数?

编辑:我刚刚发现了一个回答我想法的现有问题:What's the difference between console.dir and console.log?

【问题讨论】:

    标签: javascript html debugging coding-style


    【解决方案1】:

    信息的呈现方式不同。例如,在 Firebug 中,如果我这样做:

    a = { foo: "foo", bar: "bar" };
    

    然后我做:

    console.log(a)
    

    我明白了:

    Object { foo="foo", bar="bar"}
    

    如果我这样做:

    console.dir(a)
    

    我明白了:

    bar    "bar"
    foo    "foo"
    

    如果我有嵌套的对象,我会有一些扭曲的控件(MDN 称它们为“披露三角形”),这样我就可以轻松地深入挖掘对象属性。

    根据您使用的工具,YMMV。

    【讨论】:

    • 谢谢,以后我会更加依赖console.dir。很有趣的是,上周我在为console.log 苦苦挣扎:Chrome 不会向我显示对象的内容(也不会显示它的属性)。
    • 感谢简洁明了的解释:)
    猜你喜欢
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2013-02-13
    相关资源
    最近更新 更多