cze*_*rny 5 javascript debugging firefox javascript-debugger
在Javascript执行期间,某些变量可以被“优化”。因此,此类变量的值在调试时无法检查(用户文档)。如果尝试评估变量,变量视图将显示(优化)消息,并且控制台将引发以下错误:
Error: variable has been optimized out
Run Code Online (Sandbox Code Playgroud)
有什么方法可以在Firefox中强制执行此类变量的评估或禁用此优化?
以防止这种优化的方式使用变量。
function NOP() {}
// then in the optimised code
NOP(myvar);
// debugging here should now show `myvar`
Run Code Online (Sandbox Code Playgroud)
当一个变量被“优化掉”时,它只是意味着它没有在当前作用域的上下文中被修改。因此,JavaScript 引擎已经完成了一些优化魔法并暂时隐藏了该变量。例如,假设您正在使用 lodash 迭代某种集合。
var parentThingy = [];
var childThingy = [];
_.each (collectionThingy, function(data){
// parentThingy is not being modified inside this callback
// so it will be "optimized away" while you are inside this
// function scope.
var transformed;
if (data.someFlag) {
transformed = transformDataSomehow(data);
}
// childThingy is being modified, so you will be able to
// see its value in the debugger.
if (transformed) {
childThingy.push(transformed);
}
});
// Now that you've exited the callback scope, you will be able to see
// the value of parentThingy again.
if (childThingy.length > 1){
parentThingy.push(childThingy);
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 NOP 建议强制 parentThingy 在回调范围内可见,但由于您没有在该回调中修改 parentThingy,因此您不需要查看它。它没有改变,也不会改变。它与您当前正在调试的代码无关。退出回调的范围后, parentThingy 将再次对调试器可见。
仅供参考:这不是 Firefox 的事情。Chrome 做同样的事情,它只是使用不同的措辞来表示变量与当前作用域无关。
| 归档时间: |
|
| 查看次数: |
8301 次 |
| 最近记录: |