JavaScript eval() 解析函数字符串时出现“语法错误”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2760953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript eval() "syntax error" on parsing a function string
提问by Kenny Peng
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 (Lua), I have it as a string. The server returns the string in some JSON and I have the client interpret it using a clean-up function:
我在服务器端的配置文件中指定了一些 JavaScript 代码。由于我无法在配置语言 (Lua) 中指定 JavaScript 函数,因此我将其作为字符串。服务器以一些 JSON 格式返回字符串,我让客户端使用清理函数来解释它:
parse_fields = function(fields) {
for (var i = 0; i < fields.length; ++i) {
if (fields[i].sortType) {
sort_string = fields[i].sortType;
fields[i].sortType = eval(sort_string);
}
return fields;
}
};
So basically it just evaluates sortTypeif it exists. The problem is that Firebug is reporting a "Syntax error" on the eval()line. When I run the same steps on the Firebug console, it works with no problems and I can execute the function as I expect. I've tried some different variations: window.evalinstead of plain eval, storing the sortTypeas I've done above, and trying small variations to the string.
所以基本上它只是评估sortType它是否存在。问题是 Firebug 在线上报告了“语法错误” eval()。当我在 Firebug 控制台上运行相同的步骤时,它没有问题,我可以按预期执行该功能。我尝试了一些不同的变化:window.eval而不是 plain eval,sortType像我上面所做的那样存储,并尝试对字符串进行小的变化。
A sample value of fields[i].sortTypeis "function(value) { return Math.abs(value); }". Here's the testing I did in Firebug console:
的采样值fields[i].sortType是"function(value) { return Math.abs(value); }"。这是我在 Firebug 控制台中进行的测试:
>>> sort_string
"function(value) { return Math.abs(value); }"
>>> eval(sort_string)
function()
>>> eval(sort_string)(-1)
1
and the error itself in Firebug:
以及 Firebug 中的错误本身:
syntax error
[Break on this error] function(value) { return Math.abs(value); }
The last bit that may be relevant is that this is all wrapped in an Ext JS onReady()function, with an Ext.nsnamespace change at the top. But I assumed the window.evalwould call the global eval, regardless of any possible evalin more specific namespaces.
可能相关的最后一点是,这一切都包含在一个 Ext JSonReady()函数中,并Ext.ns在顶部更改了命名空间。但我认为window.eval会调用 global eval,而不管eval更具体的命名空间中的任何可能。
Any ideas are appreciated.
任何想法表示赞赏。
回答by jhurshman
To do what you want, wrap your string in parentheses:
要执行您想要的操作,请将字符串括在括号中:
a = "function(value) { return Math.abs(value);}";
b = eval("("+a+")");
b(-1);
回答by whatever
The parentheses are required because they force the thing inside them to be evaluated in an expression context, where it must be a function-expression.
括号是必需的,因为它们强制在表达式上下文中计算其中的内容,其中它必须是函数表达式。
Without the parentheses, it could instead be a function declaration, and it seems as if it is sometimes being parsed that way - this could be the source of the odd/inconsistent behaviour you're describing.
没有括号,它可能是一个函数声明,似乎有时会以这种方式解析 - 这可能是您所描述的奇怪/不一致行为的根源。
Compare this function declaration:
比较这个函数声明:
function foo(arg) {}
with this function-expression:
使用这个函数表达式:
var funcExpr = function foo(arg) {};
It also has to be a function-expression if it doesn't have a name. Function declarations require names.
如果它没有名称,它也必须是一个函数表达式。函数声明需要名称。
So this is not a valid declaration, because it's missing its name:
所以这不是一个有效的声明,因为它缺少它的名字:
function (arg) {}
but this is a valid, anonymous function-expression:
但这是一个有效的匿名函数表达式:
var funcExpr = function(arg) {};

