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.

Bogdan Verbenets
  • 25,686
  • 13
  • 66
  • 119

4 Answers4

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

TheBrain
  • 5,528
  • 2
  • 25
  • 26
  • 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. – Bogdan Verbenets Jul 14 '11 at 09:37
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.

Ali
  • 287
  • 2
  • 4
  • 13
  • 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 https://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/ 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. – Sigfried Mar 25 '15 at 10:53
1

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

Skay
  • 9,343
  • 6
  • 27
  • 28
  • 3
    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. – tomfumb Jul 13 '11 at 19:09
  • 1
    Templating is a great example where eval shines. And then I would call it heaven. – Lime Jul 13 '11 at 19:12
  • 1
    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. – Skay Jul 13 '11 at 19:22
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

Lime
  • 13,400
  • 11
  • 56
  • 88