JavaScript:声明vs表达式vs语句 [英] JavaScript: declarations vs expressions vs statements

查看:58
本文介绍了JavaScript:声明vs表达式vs语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几个小时中,我一直在尝试查找3之间的差异,而不仅仅是差异,我也一直在尝试找出哪些是同义词,MDN会将所有声明都称为语句",因此我假设那是真实的.但是,我读过的所有文章和SO问题都没有为我提供能够区分这3个(或2个,表达式vs语句)的备忘单.

For the past few hours I have been trying to find the difference between the 3, not just the difference, I also have been trying to find out which are sort of synonymous, MDN calls all declarations "statements", so I presume that is true. However, none of the articles and SO questions I read provided me with a cheatsheet of sorts to distinguish between the 3 (or the 2, expressions vs statements).

我在声明中注意到的一点是,它们都以某种方式涉及特殊的JavaScript关键字,例如breakforvar.文章说,表达式对某项求值,而语句则执行某项操作.那么什么是函数?它是一个语句-表达式混合体吗(因为它既在被调用时执行一个动作,又返回一个值)?现在,我假设情况并非如此,因为函数调用不包含JavaScript关键字.

Something I have noticed with statements is that they all somehow involve a special JavaScript keyword like break or for or var. The articles say that an expression evaluates to something, while a statement performs an action. What is a function then ? Is it a statement-expression hybrid (since it both performs an action when called, and returns a value) ? Right now, I am assuming that this is not the case since a function call does not involve a JavaScript keyword.

然后是声明,每个声明也是一个声明吗?

And then there are declarations, is every declaration also a statement ?

我也知道像20 + 5 * 2 / 3;这样的表达式语句,但是这些语句实际上没有用吗?为什么有人会用这样的行来污染他们的代码?

I am also aware of expression statements like 20 + 5 * 2 / 3;, but aren't these virtually useless ? Why would anyone pollute their code with a line like this ?

如果有人可以问上述问题(或至少其中一些问题),或者有人可以给我提供一些备忘单,我会感到非常高兴.为什么用如此流行的语言,关于一个主题如此简单的材料却很少?

I would be more than glad if someone could ask the above questions (or at least some of them), or if someone could provide me with some sort of a cheatsheet. Why is there so little material on a topic that appears to be so simple in such a popular language ?

推荐答案

所有这些语言(包括JS)都遵循这些术语的标准定义.我对它们的看法如下:

There are standard definitions of these terms that all languages, including JS, follow. My takes on them are the following:

  • 表达式产生一个值,并且可以将其写入需要该值的任何地方.

  • An expression produces a value and can be written wherever a value is expected.

另一方面,语句是独立的执行单元.它不返回任何内容.

A statement on the other hand is is a standalone unit of execution. It does not return anything.

声明是将值分配给变量的语句.

A declaration is a statement in which a value is assigned to a variable.

所有声明均为声明,但并非所有声明均为声明.

All declarations are statements, but not all statements are declarations.

大多数语句和所有声明都包含表达式.

Most statements and all declarations contain expressions.

Javadoc 也给出了简洁明了的定义:

Javadoc also gives nice, succinct definitions:

表达式是由变量,运算符和方法调用组成的构造,这些构造是根据语言的语法构造的,其结果为单个值.

An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.

语句大致等同于自然语言中的句子.一条语句构成了完整的执行单元.

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution.

现在,您关于必然涉及关键字的理论是不正确的.这是一个包含关键字的语句:

Now, your theory on keywords being necessarily involved is incorrect. Here is a statement that includes a keyword:

break;

这不是一个:

foo();

在第一个示例中,我执行break,而在第二个示例中,我调用一个函数.两者都是语句,但是只有一个包含任何特殊关键字.

In the first example I execute a break, whereas in the second one I call a function. Both are statements, however only one includes any special keyword.

函数不是语句.如上所述,函数 call 是一个语句.对于返回值的函数,该调用也可以是一个声明:

A function is not a statement. A function call is a statement, as demonstrated above. In the case of a function that returns something, the call can also be a declaration:

var bar = foo();

对于您的最后一个问题,这方面的资料很少的原因是,这只是语义问题.人们不会徘徊在这些术语的确切定义上.

And for your final question, the reason there is so little material out there on this is because this is merely an issue of semantics. People do not linger on the exact definitions of these terms.

为进一步说明,我建议您查看博客Axel Rauschmayer的帖子专门讨论了JS中的语句和表达式之间的区别.

For further clarification, I recommend taking a look at this blog post by Axel Rauschmayer that specifically talks about the difference between statements and expressions in JS.

这篇关于JavaScript:声明vs表达式vs语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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