JS中的表达式和语句有什么区别? [英] What is the difference between an expression and a statement in JS?
问题描述
语句和表达式有什么区别?我怎么知道每一个?
What is the difference between a statement and a expression? How can i know each one is?
为什么无效转换这个:
void function() {
}();
表达式?
为什么这是有效的:
something((something) => something + 2);
这不是:
something((something) => if (something) { something + 2 });
?
谢谢!!
推荐答案
语句和表达式是不同的语言单位。可能绊倒你的是,与其他语言不同,JavaScript具有规范所称的内容 ExpressionStatement :它可以将任何表达式视为语句(但它不能将语句视为表达式)。例如,这是有效的JavaScript:
Statements and expressions are different language units. What's probably tripping you up is that JavaScript, unlike some other languages, has what the spec calls ExpressionStatement: It can treat any expression as a statement (but it cannot treat statements as expressions). So for instance, this is valid JavaScript:
flag && console.log("Hi there");
...而在(比如)Java中无效,因为虽然Java有一些允许特定表达式事物(例如方法调用)的特定语句,它没有JavaScript的整体 ExpressionStatement 。
...whereas in (say) Java that wouldn't be valid, because while Java has some specific statements to allow for specific expression-like things (method calls, for instance), it doesn't have JavaScript's overall ExpressionStatement.
为什么
void
转换它:
void function() {
}();
表达式?
因为 void
是运算符 。它评估其操作数(后面跟随它),然后将其结果设置为 undefined
。它与您在C,C ++,Java或C#等语言中看到的 void
完全无关。所以整个事情是 ExpressionStatement 。你需要 void
的唯一原因是没有它,当解析器期望看到一个语句时,关键字 function
将解析器置于一个期望函数声明的状态,而不是表达式。 (您可以使用任何运算符,而不仅仅是 void
,将解析器置于表达式状态; 此问题及其答案中的详细信息。)
Because void
is an operator. It evaluates its operand (whatever follows it), and then sets its result to undefined
. It's not at all related to the void
that you see in languages like C, C++, Java, or C#. So the whole thing is an ExpressionStatement. The only reason you need the void
there is that without it, when the parser is expecting to see a statement, the keyword function
puts the parser into a state where it's expecting a function declaration, not an expression. (You can use any operator, not just void
, to put the parser into expression state; details in this question and its answers.)
为什么会这样:
And why this works:
something((something) => something + 2);
这不是:
something((something) => if (something) { something + 2 });
由于规范定义箭头函数的方式。箭头函数有两种形式:
Because of the way arrow functions are defined by the specification. There are two forms of arrow function:
- 具有简洁正文的那些(没有
{}
) - 身体详细的人
- Ones with a concise body (no
{}
) - Ones with a verbose body
简洁例如:
array.sort((a, b) => a - b);
等效详细示例:
array.sort((a, b) => {
return a - b;
});
简洁机构的箭头函数要求正文是表达式,因为,如上所示,它们返回表达式的结果。您不能在那里使用语句的原因与您不能使用其他地方需要表达式的语句相同:隐式 return
语句需要结果值。你的第二个例子,翻译成一个冗长的身体,是:
Arrow functions with concise bodies require that the body be an expression because, as you can see above, they return the result of the expression. You can't use a statement there for the same reason you can't use a statement other places an expression is required: A result value is needed for the implicit return
statement. Your second example, translated to a verbose body, is this:
something((something) => {
return if (something) { something + 2 };
});
...这说明了为什么它不起作用:你有 if
在之后返回
,但 return
语句需要操作数(表达式)或语句终结符。
...which demonstrates why it didn't work: You have an if
after return
, but the return
statement requires an operand (expression) or statement terminator instead.
这篇关于JS中的表达式和语句有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!