JavaScript eval() “syntax error” on parsing a function string

前端 未结 2 358
不思量自难忘°
不思量自难忘° 2020-12-09 17:23

I have a bit of JavaScript code that is specified in a configuration file on the server-side. Since I can\'t specify a JavaScript function in the configuration language (Lu

2条回答
  •  借酒劲吻你
    2020-12-09 18:02

    To do what you want, wrap your string in parentheses:

    a = "function(value) { return Math.abs(value);}";
    b = eval("("+a+")");
    b(-1);
    

提交回复
热议问题