首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Chrome控制台调试时如何更改js局部变量的值

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

Stack Overflow用户
提问于 2016-06-12 10:06:21
回答 2查看 4.7K关注 0票数 3

当我用Chrome控制台调试javascript时,我想要更改函数的局部变量。我知道如何更改全局变量的值,但在Chrome控制台调试时如何更改局部变量的值?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-12 10:08:46

您不能在Chrome控制台中进行调试。您可以在Chrome调试器中调试。如果在调试器中的断点停止,则可以使用控制台通过赋值来更改任何作用域中变量的值。

例如,打开dev工具并运行以下代码,读取注释:

代码语言:javascript
运行
复制
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();

票数 4
EN

Stack Overflow用户

发布于 2016-06-12 10:18:06

尝试设置一个断点,以便将局部变量的值重新定义为另一个值。然后,您可以访问该范围内的所有数据。

这可能会有帮助

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37772980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档