为什么console.log()没有传递变量的快照? [英] Why doesn't console.log() take a snapshot of the passed variables?

查看:289
本文介绍了为什么console.log()没有传递变量的快照?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我遇到了一些非常奇怪的行为。我想我现在知道了,但是我想知道我的想法是真的发生了,还是有其他的魔法。所以这是我的代码:

  var SomeObject = {}; 

SomeObject.foo = function(a,b){
var baz = this.bar(a,b);
console.log(baz);
console.log(baz.left);
SomeObject.magicalStuff(baz);
};

SomeObject.bar = function(a,b){
return {left:a-b,top:b-a};
};

SomeObject.magicalStuff = function(position){
position.left = 0;
};

SomeObject.foo(100,50);

jsFiddle的代码



输出结果类似(取决于浏览器):

 >对象
50

如果扩展对象(在Chrome,Safari或Firefox你得到的是:

 > Object 
left:0
顶部:-50

而我希望:

 > Object 
left:50
顶部:-50

我认为是,console.log()真的只是发布一个对控制台的引用,一旦你点击扩展符号,它就会被读取,但是不会失败console.log()作为一个调试工具的目的?
我总是期待console.log()来快照我传递给它的东西,真正让人惊讶的是看到一个实际的控制台后面的语句。 log()更改那个非常console.log()调用的输出。



还是有其他的事情?



编辑:我也想知道浏览器开发人员是否有理由认为ement console.log这样(我想有一个,否则在主流浏览器之间不一致)。

解决方案

在Firebug中有你想要的 console.dir()



一般来说,不可能打印每个级别的嵌套属性,因为对象可以包含循环引用,如 var a = {}; var b = {a:a}; ab = b;



实现一个完美的克隆方法是非常困难的 - 我猜它必须基本上只是转储整个内存,并且日志记录将花费很长时间。想想 console.log(window) ...


I've ran into some really weird behavior with javascript today. I think I got it somehow figured out now, but I'd like to know if what I think is going on is really happening or if there is some other magic involved. So this is my code:

    var SomeObject = {};

    SomeObject.foo = function(a, b) {
       var baz = this.bar(a, b);
       console.log(baz);
       console.log(baz.left);
       SomeObject.magicalStuff(baz);
    };

    SomeObject.bar = function(a, b) {
        return {left: a-b, top: b-a};
    };

    SomeObject.magicalStuff = function(position) {
        position.left = 0;
    };

    SomeObject.foo(100, 50);

The code at jsFiddle

The output of this is something like (depending on the browser):

> Object
50

If you expand the "Object" (in Chrome, Safari or Firefox (Firebug) what you get is:

> Object
    left: 0
    top: -50

Whereas I would expect:

> Object
    left: 50
    top: -50

What I think is going on is that console.log() really just "posts" a reference to the console, which gets read once you click on the "expand" symbol. But doesn't that kind of defeat the purpose of console.log() as a debugging instrument? I always expected console.log() to "snapshot" the stuff I pass to it. It is really surprising to see a statement which comes after the actual console.log() change the output of that very console.log() call.

Or is there something else going on?

Edit: I'm also wondering if there is a sound reason for browser developers to implement console.log like this (I guess there is one, otherwise it wouldn't be consistent across major browsers).

解决方案

There is console.dir() for what you want in Firebug.

In general, it is not possible to print every level of nested properties, since objects can contain circular references like var a = {}; var b = {a: a}; a.b = b;

Implementing a perfect clone method is very hard - I guess it would have to basically just dump the whole memory, and logging would take awfully long. Think about console.log(window)...

这篇关于为什么console.log()没有传递变量的快照?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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