开发者

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜