简体   繁体   English

有关Chrome和Firefox中的Console.log的问题

[英]Question regarding Console.log in Chrome and Firefox

i'm meditating about this: 我正在对此进行冥想:

console.log in chrome and firefox shows the same result, before and after sorting (the result after sorting). chrome和firefox中的console.log在排序前后(排序后的结果)显示相同的结果。 a quick test in the playcode.io editor shows the results as expected. 在playcode.io编辑器中进行的快速测试显示了预期的结果。 The alert method in chrome works also properly. chrome中的alert方法也可以正常工作。 Does anybody know about the background of this? 有人知道这个背景吗?

Found this Thread: JavaScript console.log execution order? 找到了这个线程: JavaScript console.log执行顺序?

But i don't really get it... 但是我真的不明白...

let data = [

    {A: 100232}, 
    {A: 223434},
    {A: 233434},
    {A: 645455},
    {A: 212334},
    {A: 34343},
    {A: 743434},

];

console.log(data);
data.sort(function(a, b){return b.A - a.A}); 
console.log(data);

It is not a problem, it is how the console.log works in some browsers. 这不是问题,而是console.log在某些浏览器中的工作方式。 It is printing a link to the object, and once you open this object in the console it shows a current version of the object. 它正在打印指向该对象的链接,并在控制台中打开该对象后,它将显示该对象的当前版本。 See docs : 参见文档

Don't use console.log(obj), use console.log(JSON.parse(JSON.stringify(obj))). 不要使用console.log(obj),请使用console.log(JSON.parse(JSON.stringify(obj)))。

This way you are sure you are seeing the value of obj at the moment you log it. 这样,您可以确定在记录obj时会看到它的值。 Otherwise, many browsers provide a live view that constantly updates as values change. 否则,许多浏览器会提供实时视图,该实时视图会随着值的变化而不断更新。 This may not be what you want. 这可能不是您想要的。

To see the version of the object in the moment of console logging try this: 要在控制台记录时查看对象的版本,请尝试以下操作:

let data = [

    {A: 100232}, 
    {A: 223434},
    {A: 233434},
    {A: 645455},
    {A: 212334},
    {A: 34343},
    {A: 743434},

];

console.log(JSON.stringify(data));
data.sort(function(a, b){return b.A - a.A}); 
console.log(JSON.stringify(data));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM