【发布时间】:2026-01-10 22:40:02
【问题描述】:
当我使用 Chrome 控制台调试 javascript 时,我想更改函数的局部变量。我知道如何更改全局变量的值,但是在 Chrome 控制台中调试时如何更改局部变量的值?
【问题讨论】:
标签: javascript debugging
当我使用 Chrome 控制台调试 javascript 时,我想更改函数的局部变量。我知道如何更改全局变量的值,但是在 Chrome 控制台中调试时如何更改局部变量的值?
【问题讨论】:
标签: javascript debugging
您不会在 Chrome 控制台中调试。您在 Chrome 调试器中进行调试。如果您在调试器的断点处停止,您可以使用控制台通过分配给它来更改任何范围内变量的值。
例如,打开开发工具并运行此代码,读取 cmets:
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();
【讨论】: