Chrome dev tool issue

本文探讨了Chrome开发者工具在显示局部与全局同名变量时存在的显示问题。当局部作用域内的变量与全局变量同名但未赋值时,Chrome可能会误显示全局变量为undefined。文章通过示例代码解释了这一现象,并提醒开发者不要轻易相信Chrome开发者工具中非当前作用域的变量值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Chrome dev tool display the variables with the same name the same value that is assigned in the current context, even though some of them are defined in other contexts and has been assigned different values. Take the following code as example.

var a = 0;
function func() {
    var a = 1;
}
func();

When you set a break point at the line inside the function and chrome javascript executor pauses at the break point, chrome will show an undefined on the globally defined varible a if you move the mouse point over it, even though the global varible a has been assigned value 0. The truth is, the value of global varible a is still 0, and the local varible in the func is undefined because it’s hoisted but has not been assigned. It’s probably an issue inside chrome, or chrome dev tools could not show the javascript execution context and scope chain, which lead to such an confusing value display. Some developers may think that how could the assignment to the varible defined in the function affect the varible outside the function? Yes, it doesn’t make sense and it could not be. It’s just an error display. Therefore, do not trust the varible values out of the current context inspected with mouse actions in chrom dev tool. Otherwise you will fool yourself.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值