[英]can you turn off javascript garbage collector for a debugging mode?
当我调试javascript时,我经常遇到由于范围问题而无法访问应该由闭包提供给我的变量的情况。
在这种情况下,我可以遍历堆栈级别以使自己处于正确的范围内,但这非常令人困惑,并且在处理promise / async调用时不会解决问题。
我相信这是垃圾收集器标记并清除未使用的变量的功能(如果我错了,请纠正我)。
有没有一种我可以用来保留闭包变量的模式(是的,我意识到这可能不会造成任何垃圾回收,但是在调试时仍然有用,并且不会影响应用程序的行为)
此问题的代码:
function hello(arr, foo, bar) { arr.forEach(item => { debugger; // HERE `foo`, `arr` and `bar` are reference errors in debugger evaluation }); return 1 } hello([1,2,3], 'foo', 'bar')
- - - - - - 编辑 - - - - - - -
更新示例
假设实际上实际上是由于优化决定而没有首先捕获外部作用域-这意味着不涉及垃圾回收-您可以尝试以下操作:
let singularity = null;
function blackhole(val) {
singularity = val;
}
function someScope() {
var abc = 'abc'
var arr = [1...100] // pseudocode
arr.map(n => {
debugger;
blackhole(abc); // forces abc to be captured
// or ...
eval(""); // eval leads to more conservative optimizations
})
}
要么
function someScope() {
var abc = 'abc'
var arr = [1...100] // pseudocode
// normal functions are not as easily optimized as arrow functions
arr.map(function() {
debugger;
})
}
或者,假设您使用的是firefox,请转到about:config
并设置javascript.options.baselinejit = false
。
一般的想法是强制编译器不执行会丢弃未使用的外部作用域的优化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.