小编Dus*_*tin的帖子

为什么 Chrome 开发工具说未定义变量?

在下面的代码中,当在断点处暂停时,Chrome Dev Tools 会根据 console.log 行是否被注释掉来告诉我“foo”是否被定义。

一旦在断点处,如果在控制台中键入“foo”,或者将其添加为监视变量,如果控制台语句被注释掉,它将说 foo 未定义,但是如果控制台语句没有被注释掉,那么它将正确显示 foo (1) 的值。为什么会这样?

function func1(){

    let foo = 1; 

    var func2 = function (){
        function func3 (){
            let foo2 = 4;
            // put breakpoint here
            let foo3 = 5;
            // console.log(foo);  //says foo is undefined when this line commented
        }
        func3();
    }
    func2();
}

func1();
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome google-chrome-devtools

6
推荐指数
1
解决办法
2892
查看次数