简体   繁体   English

Chrome调试器显示错误值

[英]Chrome debugger showing wrong values

I was debugging my js code on Chrome Ubuntu (Version 68.0.3440.106 64 bits) and I notice that the debugger is giving me wrong values, even if I watch the value by adding a watch expression entry. 我在Chrome Ubuntu(版本68.0.3440.106 64位)上调试js代码,并且我注意到调试器给了我错误的值,即使我通过添加监视表达式条目来监视该值也是如此。

for (let i = 0; i < visibility.show.or.length; ++i) {
   const currentVisibilityContext = visibility.show.or[i];
}

我不能不确定

but if I use var instead of let it works fine 但是如果我使用var而不是let工作正常

for (var i = 0; i < visibility.show.or.length; ++i) {
   const currentVisibilityContext = visibility.show.or[i];
}

Can any one explain to me this behavior? 有人可以向我解释这种行为吗? Thank you very much. 非常感谢你。

Variables declared with the var keyword can not have Block Scope. 用var关键字声明的变量不能具有“块作用域”。

Variables declared inside a block {} can be accessed from outside the block. 可以从块外部访问在{}内部声明的变量。

Example

{ 
    var x = 2; 
}
// x CAN be used here

Before ES2015 JavaScript did not have Block Scope. 在ES2015之前,JavaScript没有Block Scope。

Variables declared with the let keyword can have Block Scope. 用let关键字声明的变量可以具有Block Scope。

Variables declared inside a block {} can not be accessed from outside the block: 在块{}中声明的变量不能从该块外部访问:

Example

{ 
    let x = 2;
}
// x can NOT be used here

copied without problems from W3schools ofc 从W3schools ofc复制而没有问题

That's so weird. 太奇怪了 I get this when a console snippet is fired. 当控制台代码段被触发时,我得到了这个。 在此处输入图片说明

Is any of the properties of visibility, a observable property (like a KO observable) by any chance. 能见度的任何属性, observable属性(例如KO可观察的)是否是偶然的。 Probably the debugger stepped into the observable property viz, function and is out of scope. 调试器可能进入了可观察的属性,即功能,超出了范围。 Since everything is in one line, you get to see that debugger is executing a single command. 由于所有内容都在一行中,因此您可以看到调试器正在执行单个命令。 Please try to split the line into multiple lines, to see how it behaves. 请尝试将线分成多行,以查看其行为。

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

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