当我用Chrome控制台调试javascript时,我想要更改函数的局部变量。我知道如何更改全局变量的值,但在Chrome控制台调试时如何更改局部变量的值?
发布于 2016-06-12 10:08:46
您不能在Chrome控制台中进行调试。您可以在Chrome调试器中调试。如果在调试器中的断点停止,则可以使用控制台通过赋值来更改任何作用域中变量的值。
例如,打开dev工具并运行以下代码,读取注释:
function foo() {
var bar = 42;
// Normally, you don't have to use a hardcoded breakpoint like
// the one that follows, you can set a breakpoint from within the
// debugger just by navigating to the line of code and clicking in
// the left-hand gutter. But in Stack Snippets the easiest way to
// do one is to use the debugger statement:
debugger;
// Now, when stopped on the breakpoint, type this in the console:
// bar = 67;
// ...and press Enter.
// Then hit the arrow button to allow the script to continue
console.log(bar); // ...and this will log 67 instead of 42.
}
foo();
发布于 2016-06-12 10:18:06
尝试设置一个断点,以便将局部变量的值重新定义为另一个值。然后,您可以访问该范围内的所有数据。
这可能会有帮助
https://stackoverflow.com/questions/37772980
复制相似问题