Chrome的JavaScript控制台懒惰有关评估数组? [英] Is Chrome's JavaScript console lazy about evaluating arrays?

查看:161
本文介绍了Chrome的JavaScript控制台懒惰有关评估数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会用code启动:

var s = ["hi"];
console.log(s);
s[0] = "bye";
console.log(s);

简单的,对不对?响应于此,萤火虫表示:

Simple, right? In response to this, Firebug says:

["hi"]
["bye"]

美好的,但Chrome的JavaScript控制台(7.0.517.41 Beta版)说:

Wonderful, but Chrome's JavaScript console (7.0.517.41 beta) says:

["bye"]
["bye"]

难道我做错了什么事,或者Chrome的JavaScript控制台是非常懒惰有关评估我的阵列?

Have I done something wrong, or is Chrome's JavaScript console being exceptionally lazy about evaluating my array?

推荐答案

感谢您的评论,TEC。我能找到一个未经证实存在WebKit的错误,说明此问题:<一href=\"https://bugs.webkit.org/show_bug.cgi?id=35801\">https://bugs.webkit.org/show_bug.cgi?id=35801

Thanks for the comment, tec. I was able to find an existing unconfirmed Webkit bug that explains this issue: https://bugs.webkit.org/show_bug.cgi?id=35801

似乎有关于它到底有多少这是一个bug,以及是否可以解决的一些争论。它似乎是不好的行为给我。因为,在Chrome中至少它时,code所在区域(在加载页面之前)被立即执行脚本,即使控制台是开放的,每当页面刷新时它是特别麻烦给我。调用的console.log当控制台还没有到对象的引用活跃不仅导致排队,而不是输出控制台将包含。因此,阵列(或任何对象),将不被计算,直到控制台已准备就绪。这真的是懒惰的评价的情况。

There appears to be some debate regarding just how much of a bug it is and whether it's fixable. It does seem like bad behavior to me. It was especially troubling to me because, in Chrome at least, it occurs when the code resides in scripts that are executed immediately (before the page is loaded), even when the console is open, whenever the page is refreshed. Calling console.log when the console is not yet active only results in a reference to the object being queued, not the output the console will contain. Therefore, the array (or any object), will not be evaluated until the console is ready. It really is a case of lazy evaluation.

然而,有一个简单的方法来避免这种情况在code:

However, there is a simple way to avoid this in your code:

var s = ["hi"];
console.log(s.toString());
s[0] = "bye";
console.log(s.toString());

通过调用toString,你在内存中创建一个重新presentation不会通过下面的语句,这时候它准备控制台将读取改变。控制台输出是直接传递对象略有不同,但似乎可以接受的:

By calling toString, you create a representation in memory that will not be altered by following statements, which the console will read when it is ready. The console output is slightly different from passing the object directly, but it seems acceptable:

hi
bye

这篇关于Chrome的JavaScript控制台懒惰有关评估数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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