简体   繁体   English

为什么开发工具不理解闭包?

[英]Why dev tools don't understand closures?

Can someone explain me why Dev Tools of Chrome (maybe other browsers as well - I haven't bothered with checking) don't see variables up the chain? 有人可以解释一下为什么Chrome的开发工具(也许是其他浏览器 - 我没有打扰过检查)没有看到链中的变量?

so if I have something like this: 所以,如果我有这样的事情:

function wholeKit(kaboodle) {
    kaboodle.forEach((k)=> {
      k.parts.map((p)=> {
         // console.log(kaboodle, k)    
        debugger;   
      })   
    })  
}

At the breakpoint Chrome will see value of p , but won't see kaboodle and k . 在断点处Chrome会看到p值,但不会看到kaboodlek They would be undefined . 它们将是undefined But if I uncomment the line above the breakpoint - magically those variables will be no longer undefined. 但是,如果我取消断开断点上方的行 - 神奇地说这些变量将不再是未定义的。 How so? 怎么会这样?

jsbin for those who can't mentally digest ES6 jsbin适合那些无法精神消化ES6的人

upd: apparently that's how browser's garbage collector works, those variables that not being used get scraped. upd:显然这就是浏览器的垃圾收集器的工作原理,那些未被使用的变量被刮掉了。 Now I'm curious if there's a way to tweak GC, for when DevTools are open? 现在我很好奇是否有办法调整GC,因为当DevTools打开时?

Your code has syntax errors. 您的代码有语法错误。 The forEach and map are missing a closing parenthesis and Chrome does not support the ES6 arrow syntax for lambdas. forEach和map缺少右括号,Chrome不支持lambda的ES6箭头语法。

When I changed the callbacks to regular functions and added the missing parenthesis everything worked just fine. 当我将回调更改为常规函数并添加了缺少的括号时,一切正常。

function wholeKit(kaboodle) {
    kaboodle.forEach(function(k){
        k.parts.map(function(p){
            console.log(kaboodle, k)    
            debugger;   
        });
    });
}

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

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