javascript 评估

发布于 2024-08-14 11:48:23 字数 130 浏览 2 评论 0原文

eval('({"suc":true})')

上面的说法是错误的,应该是:

eval('{"suc":true}')

为什么?

eval('({"suc":true})')

The above is wrong,should be:

eval('{"suc":true}')

Why?

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

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

发布评论

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

评论(4

℡Ms空城旧梦 2024-08-21 11:48:23

当尝试评估时,解释器会看到大括号并认为它是一个块开头。将其括在括号中使其成为表达式并正确初始化对象。

When trying to evaluate the interpreter sees the curly brace and thinks it is a block beginning. Enclosing in parenthesis makes it an expression and initializes an object correctly.

假装爱人 2024-08-21 11:48:23

我不知道你想实现什么,但从你的例子来看,第一个是正确的,第二个抛出语法错误。

eval('({"suc":true})')({"suc":true}) 相同,JavaScript 将其解释为:

( // <- this states begining of expression
    { // <- this is hash/object literal begining
        "suc": // <- this is property name, given as string
            true // <- this is value
    }
)

所以它返回具有 suc 属性和关联值 true 的新对象。

eval('{"suc":true}'){"suc":true} 相同,解释为:

{ // <- this is block begining
    "suc": // <- this is label, but incorrect, as it is given as string, not literally
        true // <- this is expression
}

如果更改 "suc "suc (不带括号),那么它就可以工作,但它与第一​​个示例不同。

更新

至于为什么数组不需要括号:JavaSript 中除了数组之外没有其他以 [ 字符开头的构造。

如果 { 出现在需要这样的值的上下文中,那么它不会有问题:

eval('var a = {"succ": true}')

它在源代码中是相同的(因此不仅在 eval 块中):您不能使用短创建对象符号 ({ .. }) 而不将其分配给某个变量或作为值传递(到函数、返回语句...)。

I don't know what you want to achieve, but from your examples first is correct and the second throws syntax error.

eval('({"suc":true})') is the same as ({"suc":true}) and JavaScript interprets it as:

( // <- this states begining of expression
    { // <- this is hash/object literal begining
        "suc": // <- this is property name, given as string
            true // <- this is value
    }
)

So it returns new object with suc property and associated value true.

eval('{"suc":true}') is the same as {"suc":true} and is interpreted as:

{ // <- this is block begining
    "suc": // <- this is label, but incorrect, as it is given as string, not literally
        true // <- this is expression
}

If you change "suc" to suc (without parentheses), then it would work, but it's not the same as first example.

UPDATE:

As to why array doesn't need parentheses: there is no other construct in JavaSript which would start with [ character other than array.

There would be no problem with { if it would show up in context which expects value like this:

eval('var a = {"succ": true}')

It's the same in source code (so not only in eval block): you can't create object using short notation ({ .. }) without assigning it to some variable or passing as value (to function, return statement...).

时光倒影 2024-08-21 11:48:23
eval('({"suc":true})')

其实这并没有错,会正确评价的。

eval('({"suc":true})')

Thats not wrong actually, it will be evaluated properly.

皓月长歌 2024-08-21 11:48:23

您是否尝试过使用 JSON.parse('{"suc":true})) 来代替?

Have you tried to use JSON.parse('{"suc":true})) instead?

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