在Chrome浏览器调试时是否可以更改JavaScript变量值? [英] Is it possible to change javascript variable values while debugging in Google Chrome?

查看:2629
本文介绍了在Chrome浏览器调试时是否可以更改JavaScript变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试一个JavaScript应用程序(使用Chrome开发工具),而我想在编写代码时更改一些变量值。

I'm debugging a javascript app (using Chrome dev tools), and I would like to change some variable values while stepping through the code.

所有?

我已经尝试过,并且得到了一些类似的东西:

I have tried and got something like:

> modeline
1
> modeline=0
0             <<< seems to work but... 
> modeline
1             <<< ups!!

但是我找不到任何可以或不能做的文档。

But I'm unable to find any documentation that states what can or can't be done...

推荐答案

为什么这个答案还是得到upvote?



MikaëlMayer的答案,这不再是一个问题,我的答案已经过时( go() 现在返回 30 ,然后使用控制台)。根据错误报告链接,这是在2013年7月修复的以上 gabrielmaldi的评论。它告诉我,我仍然得到upvote - 让我认为upvoter不明白的问题或我的答案。

Why is this answer still getting upvotes?

Per Mikaël Mayer's answer, this is no longer a problem, and my answer is obsolete (go() now returns 30 after mucking with the console). This was fixed in July 2013, according to the bug report linked above in gabrielmaldi's comment. It alarms me that I'm still getting upvotes - makes me think the upvoter doesn't understand either the question or my answer.

我将离开我的原始答案在这里由于历史原因,但继续Mikaël的答案而不是

I'll leave my original answer here for historical reasons, but go upvote Mikaël's answer instead.

诀窍是您无法直接更改局部变量,但可以修改对象的属性。您还可以修改全局变量的值:

The trick is that you can't change a local variable directly, but you can modify the properties of an object. You can also modify the value of a global variable:

var g_n = 0;
function go()
{
    var n = 0;
    var o = { n: 0 };
    return g_n + n + o.n;  // breakpoint here
}

控制台:

> g_n = 10
  10
> g_n
  10
> n = 10
  10
> n
  0
> o.n = 10
  10
> o.n
  10

检查 go()在设置断点并在控制台中运行这些调用后,您会发现结果是20,而不是0(可悲的是,不是30)。

Check the result of go() after setting the breakpoint and running those calls in the console, and you'll find that the result is 20, rather than 0 (but sadly, not 30).

这篇关于在Chrome浏览器调试时是否可以更改JavaScript变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆