变量值更改时中断

发布于 2024-12-12 06:28:13 字数 574 浏览 4 评论 0原文

与此处的其他问题类似,喜欢这个

有没有办法在任何 JavaScript 调试器中中断变量值的更改? (如 IE 开发工具、Visual Studio 或 Firebug)?

我猜它类似于“监视变量”,但我希望能够看到调用堆栈并在变量实际发生更改时暂停它。

另一种方法可能是使用自定义设置器覆盖值设置,并在其中放置一个断点,但不幸的是,这对 IE AFAIK 不起作用。

更新 看来这种类型的行为至少适用于非托管代码 用 C++ 编写 所以我想也许用 C++ 编写的 javascript 引擎(Google 的 V8)可能有类似的东西,但这似乎也没有我想要的。

Similar to other questions here, like this one.

Is there a way to break on the change of a variable value in any JavaScript debugger? (like IE Developer tools, Visual Studio, or Firebug)?

I guess it's something like a "watch variable", but I want to be able to see the callstack and pause it when the change to the variable actually occurs.

An alternative approach might be to override the value setting with a custom setter, and put a breakpoint in that, but unfortunately that won't work for IE AFAIK.

UPDATE
It appears that this type of behavior is available at least for unmanaged code written in C++ So I thought maybe a javascript engine written in C++ (Google's V8) might have something similar, but that doesn't appear to have what I want either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡月浅 2024-12-19 06:28:13

您甚至不需要 IDE - 您可以使用“Object.watch()”:

Object.Watch 教程

如果您使用任何一种调试器,我强烈推荐 Firebug。满足您所有 Javascript、HTML 和 CSS 需求:-):

http://getfirebug.com/javascript

== =================================================== =======

2019 年更新:

  • Object.Watch 是古老的历史。巧合的是,它在大多数现代浏览器中都不可用。

  • 最近我个人最喜欢的 JS 调试工具是 Chrome 开发者工具.

  • 我个人最喜欢的 JS IDE(适用于 Angular、.Net Core 等)是 Microsoft Visual Studio Code (MSVC)。

  • 您可以使用 Chrome 调试器执行几乎任何“预期”调试操作 - 包括设置监视(就像使用 FF Firebug 一样)。

  • Chrome 调试器与 MSVC IDE 集成良好。

  • 两者都是“免费的”(至少“像啤酒一样免费”);两者都在 Windows、Mac 和 Linux 上运行良好。

You don't even need an IDE - you can use "Object.watch()":

Object.Watch Tutorial

If you use any one debugger, I'd strongly recommend Firebug. For all your Javascript, HTML and CSS needs :-):

http://getfirebug.com/javascript

===========================================================

Update for 2019:

  • Object.Watch is Ancient History. Uncoincidentally, it's unavailable in most contemporary browsers.

  • My personal favorite JS debugging tool these days is Chrome Developer Tools.

  • My personal favorite JS IDE (for Angular, .Net Core, etc) is Microsoft Visual Studio Code (MSVC).

  • You can do just about any "expected" debugging operation - including set watches - with the Chrome debugger (just as you could with FF Firebug).

  • Chrome debugger is well integrated with the MSVC IDE.

  • Both are "free" (at least "free as in beer"); both run well on Windows, Mac and Linux.

允世 2024-12-19 06:28:13

我在 Chrome 中使用这个库取得了成功,它看起来支持所有主要浏览器。

https://gist.github.com/eligrey/384583

只需包含 .js 文件,然后调用:

yourObject.watch('someProperty', function() { 
    doWhatYouWant(); 
    debugger; 
    console.write('this too'); 
    alert('Object Changed'); //etc 
});

I'm having success with this library in Chrome and it looks to support all major browsers.

https://gist.github.com/eligrey/384583

Just include the .js file, then call:

yourObject.watch('someProperty', function() { 
    doWhatYouWant(); 
    debugger; 
    console.write('this too'); 
    alert('Object Changed'); //etc 
});
奈何桥上唱咆哮 2024-12-19 06:28:13

我不知道我是否误解了你的问题。如果你想在 Chrome 开发者工具的 js 调试会话中观察一个表达式并在它达到某个值时停止,这是相当简单的。

您只需在要检查的值所在的行上放置一个断点,然后单击鼠标右键并选择“编辑断点...”。将弹出一个对话框,提示输入表达式,当其值为 true 时,执行将停止。

例如,假设您有一个循环,并且要向其中的变量添加一个单位,并希望在变量等于 3 时停止执行。循环中的表达式如下所示:

n = i++;

您必须在该行上设置断点,并且要观察的表达式(在“编辑断点...”提示后)将为 n == 3。运行代码时,当变量达到该值时,它将停止。

您会注意到您的条件已设置,因为断点变为橙色而不是蓝色。

I don't know if I misunderstood your question. If you want to watch an expression and stop when it reaches a certain value while in a js debugging session in Chrome Developer Tools, it's rather trivial.

You can simply put a breakpoint on the line where the value you want to check is, then click with right mouse button on it and select "Edit breakpoint...". A dialog will pop up prompting for an expression, where execution will stop when its value its true.

For instance, let's say you have a loop and you are adding one unit to a variable inside it and want to stop execution when the variable equals to 3. The expression in loop would look like this:

n = i++;

You must set your breakpoint on that line and the expression to watch (after prompted by "Edit breakpoint...") would be n == 3. When running your code it will stop there when your variable reaches that value.

You'll notice your condition is set because your breakpoint turns orange instead of blue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文