大括号对象表示法在任何表达式中有效吗? [英] Is the curly brackets object notation valid in any expression?

查看:124
本文介绍了大括号对象表示法在任何表达式中有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在分析Java语言.看起来您可以将许多概念归为一个基本类型,即表达式.甚至函数参数和定义,以及字符串,数字和数学表达式也适用于该组. 唯一不合逻辑的例外是在废话一样的上下文中使用大括号对象表示法.

I'm currently analyzing the Javascript language a bit. It looks like you could group at lot of the concepts into a base type called expression. Even function arguments and definitions fit into that group, as well as strings, numbers and mathematical expressions. The only illogical exception was the curly bracket object notation in a nonsense alike context.

由于函数由多个表达式组成,因此以下代码有效:

As functions consist of several expressions the following code is valid:

function valid(){
    /\W/;
    "ahll";
    var alpha;
    alpha;
    alpha={"first": 90, "second": 80};
    alpha;
    0, {"first": 90, "second": 80};
    [1,2,3];
    alpha;
    2+3;
    new RegExp("/\W/");
    return true;
}

以下代码也应有效,但第二行会收到"missing; before statement"语法错误:

By intention the following code should be valid too, but gets a "missing ; before statement" syntax error for the second line:

function invalid(){
    {"first": 90, "second": 80};
    return true;
}

除接受允许使用花括号代码块的其他情况外,在接受表达式的其他所有情况下都接受花括号对象表示法.

The curly bracket object notation is accepted in every other case where expressions are accepted, except for these cases where a curly bracket code block would be allowed too.

上述语法错误是由javascript的实现或规范引起的吗?

Is the syntax error mentioned above caused by the implementation or the specification of javascript?

这样的废话有更准确的名字吗?

Is there a more precise name for such nonsense expression?

推荐答案

上述语法错误是由javascript的实现或规范引起的吗?

Is the syntax error mentioned above caused by the implementation or the specification of javascript?

按照规范.

这种无意义的表达是否有更精确的名称

Is there a more precise name for such nonsense expression

您正在寻找术语Expression Statement.正如您所说,对象文字是表达式(甚至主表达式),就像其他大多数东西一样.它们可以出现在许多上下文中,例如函数参数,运算符的操作数或放在方括号内.

You're looking for the term Expression Statement. As you say, Object literals are expressions (even primary expressions), just as most other things are. They can appear in many contexts, like function arguments, operands of an operator or inside brackets.

但是,函数主体-代码-不包含表达式,而是包含语句 .这意味着诸如if语句,循环语句或普通块之类的东西.或表达式语句",它只不过是要评估的表达式(而且有副作用,它们大多不是废话").

However, a function body - code - does not consist of expressions, it does consist of statements. That means things like if-statements, loop statements or plain blocks. Or "expression statements", which are nothing but an expression to be evaluated (and with side effects, they mostly are not "nonsense").

但是,规范要求:

ExpressionStatement:[超前∉{{function}] Expression ;

ExpressionStatement: [lookahead ∉ {{, function}] Expression ;

注意:ExpressionStatement不能以开头的卷曲开始 大括号,因为这可能会使它与模棱两可.还有, ExpressionStatement不能以function关键字开头,因为 可能会使它与FunctionDeclaration变得模棱两可.

NOTE: An ExpressionStatement cannot start with an opening curly brace because that might make it ambiguous with a Block. Also, an ExpressionStatement cannot start with the function keyword because that might make it ambiguous with a FunctionDeclaration.

这篇关于大括号对象表示法在任何表达式中有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆