javascript eval method

只愿长相守 提交于 2019-12-01 12:28:22

问题


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.


回答1:


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




回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/6684046/javascript-eval-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!