2

How can I watch variable values inside of javascript eval() method? And is it possible to "step into" and "step over" in eval method? For example, with a code like this:

eval("if (true) { var a = 10; a += 20; alert(a); }");

I am more interested in debugging in IE9, but I would like to hear general principle as well.

2
  • 3
    You have to tell us what your debugging environment is for us to have any chance of answering the question. Commented Jul 13, 2011 at 18:54
  • 1
    @Paul Sonier I debug in IE9, but I would like to hear advices for other browsers as well. Commented Jul 13, 2011 at 22:29

4 Answers 4

2

you can't inside the eval method. the code you give it is no code but a string. after the eval() then it becomes code and you can inspect it. This also depends on what tools you use to debug your script. In Chrome if you click Pause on exception, and execute your eval. it will break (because b is undefined) and jump in the evaluated code where you can step over, in and out. you can also use new Function("code goes here")() to evaluate code

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

1 Comment

Yeah, it appears that I can simply step into eval code in IE9 using F11. Also, IE9 has a list of other eval scripts in a combobox at the scripts tab.
1

if variable is not exist how could you know it's value? - noway. And... eval === evil.

3 Comments

wow I really hate this eval is evil crap. "...when used appropriately [it] allows for the creation of some fantastic pieces of code that wouldn’t be possible otherwise" - John Resig in 'Secrets of the JavaScript Ninja' (who knows way more about JS than any of us). Appropriately is the keyword here, and the overwhelming majority of times eval is not appropriate, but please don't just regurgitate this misnomer.
Templating is a great example where eval shines. And then I would call it heaven.
2tomfumb: 1) John Resig is not a god. And there are a lot of talented guys around; 2) maybe Resig is right, but it's not applicable in this particular case.
1

It is possible. you have to use SourceMap with your source code. Take a look at the source map which has en extensive information about this technique.

1 Comment

Most of what I've found on source maps is directed at minified code or CoffeeScript and it all made my head spin. I was about to give up on making my eval'ed code easily accessible to the debugger until I followed the link above and then another, and ended up at blog.getfirebug.com/2009/08/11/… and saw this: //@ sourceURL=foo.js. I put it at the end of my eval code in Chrome and the code shows up as foo.js in the debugger sources panel.
0

Although not thoroughly documented or advised, you can watch local variables.

var reference;

function alerta(){
  alert(reference[0]);
}

// Desired to be watched must be called as function arguments
(function(a){
  reference = arguments;

  a = 'taco';
  alerta();
  a = 'updatedValue';
  alerta();

})()

http://jsbin.com/oragan

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.