JavaScript eval()“语法错误”解析函数字符串

发布于 2024-08-31 01:04:16 字数 1252 浏览 0 评论 0原文

我有一些在服务器端的配置文件中指定的 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;
    }
}; 

所以基本上它只是评估 sortType (如果存在)。问题是 Firebug 在 eval() 行报告“语法错误”。当我在 Firebug 控制台上运行相同的步骤时,它工作没有问题,并且我可以按预期执行该函数。我尝试了一些不同的变体:window.eval而不是普通的eval,像我上面所做的那样存储sortType,并尝试小字符串的变体。

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

以及 Firebug 中的错误本身:

syntax error
[Break on this error] function(value) { return Math.abs(value); }

最后一点可能相关的是,这一切都包含在 Ext JS onReady() 函数中,并带有 Ext.ns 命名空间在顶部发生变化。但我假设 window.eval 会调用全局 eval,而不管更具体的命名空间中任何可能的 eval。

任何想法表示赞赏。

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:

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 sortType if 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.eval instead of plain eval, storing the sortType as I've done above, and trying small variations to the string.

A sample value of fields[i].sortType is "function(value) { return Math.abs(value); }". Here's the testing I did in Firebug console:

>>> sort_string
"function(value) { return Math.abs(value); }"
>>> eval(sort_string)
function()
>>> eval(sort_string)(-1)
1

and the error itself in 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.ns namespace change at the top. But I assumed the window.eval would call the global eval, regardless of any possible eval in more specific namespaces.

Any ideas are appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡忘如思 2024-09-07 01:04:16

要执行您想要的操作,请将字符串括在括号中:

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

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

a = "function(value) { return Math.abs(value);}";
b = eval("("+a+")");
b(-1);
听闻余生 2024-09-07 01:04:16

括号是必需的,因为它们强制在表达式上下文中计算其中的内容,其中它必须是函数表达式。

如果没有括号,它可能是一个函数声明,并且似乎有时会以这种方式进行解析 - 这可能是您所描述的奇怪/不一致行为的根源。

比较这个函数声明:

function foo(arg) {}

与这个函数表达式:

var funcExpr = function foo(arg) {};

如果它没有名称,它也必须是一个函数表达式。函数声明需要名称。

所以这不是一个有效的声明,因为它缺少它的名称:

function (arg) {}

但这是一个有效的匿名函数表达式:

var funcExpr = function(arg) {};

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) {};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文