开发者

Need lexical context in eval for Javascript

开发者 https://www.devze.com 2023-01-16 09:57 出处:网络
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

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

精彩评论

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