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

查看:53
本文介绍了在 Google 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...

推荐答案

为什么这个答案仍然获得赞成票?

根据 Mikaël Mayer 的回答,这不再是问题,我的回答已经过时(go() 现在返回 30 在与控制台混杂之后).根据链接的错误报告,此问题已于 2013 年 7 月修复以上 加布里埃尔马尔迪的评论.这让我感到震惊,我仍然得到了赞成票 - 让我认为赞成票的人既不理解问题,也不理解我的答案.

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).

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

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