Need lexical context in eval for Javascript
I am writing a string parser that takes input like "{x} + 20" and evaluates the lexical value of x with the sum of 20 and returns. So
开发者_运维百科var x = 10;
var new = eval("{x} + 20".replace("/\{(\w+\}/g", "$1"));
and here "new" should equal 30.
How can this be done?
I believe you just do this:
var x = 10
var y = eval("x + 20")
No?
精彩评论