且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Chrome控制台中调试时更改js局部变量的值

更新时间:2023-02-22 18:04:33

您不要在Chrome控制台中调试。您可以在Chrome调试器中执行调试。如果您在调试器中的断点处停止,您可以使用控制台通过分配来更改任何范围内变量的值。

You don't debug in the Chrome console. You do debug in the Chrome debugger. And if you are stopped at a breakpoint in the debugger, you can use the console to change the value of any in-scope variable by assigning to it.

例如,打开开发工具并运行此代码,阅读注释:

For instance, open dev tools and run this code, reading the comments:

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();