問題描述
可能重復:
Chrome 的 JavaScript 控制臺是否懶于評估數組?
我嘗試以下代碼:
var myList = new Object();
var item = new Object();
item.text = "item-1";
myList[3] = item;
console.log(myList);
console.log(myList[3].text);
// Assign another object to the same entry
var item2 = new Object();
item2.text = "item-2";
myList[3] = item2;
console.log(myList);
console.log(myList[3].text);
結果很奇怪:
* Object
* 3: Object
text: "item-2"
item-1
* Object
* 3: Object
text: "item-2"
item-2
但是 - 如果我在一段時間后執行第二部分(使用 setTimeout)并展開第一個對象,我做對了,即:
BUT - if i execute the second part after some time (using setTimeout), and unfold the first object, I get it right, i.e.:
* Object
* 3: Object
text: "item-1"
item-1
* Object
* 3: Object
text: "item-2"
item-2
我覺得分享它很重要,因為我認為人們可能會浪費大量時間來嘗試理解他的代碼中的問題.如果有人提到了一個開放的錯誤或其他東西 - 請回復這張票.謝謝!
I find it important to share it, since I think one can waste a lot of time trying to understand what's wrong in his code. And if somebody has some reference to an open bug or something - please reply this ticket. Thanks!
推薦答案
我的觀點是,這是一個非常煩人的功能",我真的希望我可以關閉它,它使調試成為一場噩夢,不知道在什么時候時間可能已經更新了一個對象,同時試圖在代碼中的給定點建立精確的對象狀態.該功能可能對觀察點"等有用,但不適用于所謂的日志"(線索就在名稱中).
My view is that this is a horrendously irritating 'feature' that I really wish I could turn off, it makes debugging a nightmare, not knowing at which point in time something may have updated an object, whilst trying to establish exact object state at a give point in the code. The feature could be useful for 'watch points' etc, but not in something called a 'LOG' (the clue is in the name).
考慮這個代碼片段:
var person = {'name':'Tom'};
console.log( person); //output the entire object variable
person.name = 'Thomas';
//the output is an object, whose 'name' value is 'Thomas', even though the log statement was placed before the value was changed to 'Thomas'.
然后:
var person = {'name':'Tom'};
console.log( person.name); //changed to output a string variable
person.name = 'Thomas';
//the output here, however, has not dynamically updated and correctly outputs 'Tom'
這篇關于console.log 中的錯誤?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!