最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

debugging - javascript eval method - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Jul 14, 2011 at 9:34 Bogdan Verbenets asked Jul 13, 2011 at 18:53 Bogdan VerbenetsBogdan Verbenets 27k13 gold badges72 silver badges121 bronze badges 2
  • 3 You have to tell us what your debugging environment is for us to have any chance of answering the question. – Paul Sonier 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. – Bogdan Verbenets Commented Jul 13, 2011 at 22:29
Add a ment  | 

4 Answers 4

Reset to default 2

you can't inside the eval method. the code you give it is no code but a string. after the eval() then it bees 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

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

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.

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./oragan

发布评论

评论列表(0)

  1. 暂无评论