3

Can a JavaScript debugger do "when this variable is read", break point there, like a C / C++ debugger?

I am calling some library function, and deep down, it is finally doing something with that argument, and instead of manually finding where it is actually using that value, can I say, stop the code when the variable is accessed?

5 Answers 5

5

Try defining a getter for your variable. A getter is a piece of code that runs whenever someone accesses an object's value. (This relatively new JavaScript feature is not available in IE, but if you're just using it for debugging in Firefox it should be fine.)

Your getter may simply return the value, but you can set a breakpoint in your getter. Or, even better, you can have the getter run the debugger statement to halt automatically.

Sign up to request clarification or add additional context in comments.

1 Comment

Note: Using console.trace(); after the debugger; statement would help to see the stack trace of what code path was trying to access/change the variable.
0

Dan Fabulich does have a more solid solution with the accessor method.

But I recently noticed that newer versions of Firebug have the ability to break on property changes. Find it in the DOM tabs.

Comments

0

With Firefox and Firebug, you can actually set a breakpoint on property change. Set a breakpoint where that variable is created and then find it in the watch section and then set "Break on property change" on that property by right-clicking on it.

Comments

0

You can set breakpoints in Firebug.

4 Comments

This doesn't really answer his question
It doesn't answer his question because he's not trying to stop on a particular line of code; he's trying to stop when a variable is accessed. Breakpoints alone won't help with this.
Why not explain your answer OrbMan
My answer was written before the question was edited to say he essentially does not want to set a manual breakpoint. I am saying that you can manually set a breakpoint with Firebug. If you want to modify your code to make things easier, Dan Fabulich's answer is a good approach,
-1

I'm not aware of any way to do what you're asking, but can you try passing a value which is likely to break the library code? Then you'll get an exception and that can be seen in any JS debugger.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.