简体   繁体   English

为什么函数声明不是变量声明时的声明

[英]Why is a function declaration not a statement while a variable declaration is

It seems like a stupid question but I really can't wrap my head around it. 这似乎是一个愚蠢的问题,但我真的无法绕过它。 I began to wonder this when I was thinking of why there is a semicolon after variable declaration but not function declaration. 当我想到为什么在变量声明之后有分号而不是函数声明时,我开始怀疑这一点。 So does that mean a function declaration is an expression? 这是否意味着函数声明是一个表达式?

And what is the official meaning for statement? 声明的官方含义是什么? I've been finding the answer saying that a statement is a "command" that tells computer what to do, but isn't a function declaration telling the computer what to do? 我一直在找答案,说一个语句是一个“命令”,告诉计算机该做什么,但不是一个函数声明告诉计算机该怎么做? Does it have something to do with when it's get loaded when executing the code? 它是否与执行代码时加载时有关?

A variable declaration can have side-effects (eg, var x = alert(42); ). 变量声明可能有副作用(例如, var x = alert(42); )。

A function declaration cannot. 函数声明不能​​。

I began to wonder this when I was thinking of why there is a semicolon after variable declaration but not function declaration 当我想到为什么在变量声明之后有分号而不是函数声明时,我开始怀疑这一点

Not every statement ends with a semicolon . 并非每个语句都以分号结尾

Examples of statements with trailing semicolon: 带分号的语句示例:

  • Variable declaration: var foo = <expression>; 变量声明: var foo = <expression>;
  • Expression statement: <expression>; 表达式语句: <expression>;
  • Do while loop: do <statement> while (<expression>); while while循环: do <statement> while (<expression>);

Examples without trailing semicolons: 不带分号的示例:

  • If statement: if (<expression>) <statement> if语句: if (<expression>) <statement>
  • Try catch statement: try <block> catch (<identifier>) <block> 尝试catch语句: try <block> catch (<identifier>) <block>
  • For loop: for (...) <statement> For循环: for (...) <statement>
  • Block statement: { <statement list> } 阻止声明: { <statement list> }

So does that mean a function declaration is an expression? 这是否意味着函数声明是一个表达式?

No, it's more complicated than that. 不,这比那更复杂。 Function declarations are not statements. 函数声明不是语句。 They are not expressions either, they are source elements . 它们也不是表达式,它们是源元素 Statements and function declarations are both source elements. 语句和函数声明都是源元素。 ** **


And what is the official meaning for statement? 声明的官方含义是什么?

I can't tell you the official definition, but in the context of JS I would say something like "it's an instruction that does not produce an assignable result/value". 我不能告诉你官方定义,但在JS的上下文中我会说“它是一个不产生可赋值结果/值的指令”。 Expressions on the other hand produce a result that can be used in other expressions. 另一方面,表达式产生可以在其他表达式中使用的结果。


** Good to know, but a bit off-topic: Since function declarations are not statements, they are technically not allowed to be used inside blocks. **很高兴知道,但有点偏离主题:由于函数声明不是语句,因此技术上不允许在块内使用它们。 This becomes even more apparent if we also consider hoisting . 如果我们也考虑吊装,那就更加明显了。 This example should throw a syntax error in every browser, but unfortunately it doesn't. 这个例子应该在每个浏览器中抛出语法错误,但不幸的是它没有。

if (true) {
  function foo() { alert('foo'); }
} else {
  function foo() { alert('bar'); }
}
foo();

This leads to different behaviors in different browser. 这导致不同浏览器中的不同行为。 While Chrome will show bar , Firefox will show foo . 虽然Chrome会显示bar ,但Firefox会显示foo Chrome just hoists both function declarations, and the second overrides the first one. Chrome只提升两个功能声明,第二个覆盖第一个。 Firefox interprets both declarations as something like a function expression . Firefox将两种声明都解释为函数表达式

Try it yourself in different browsers. 在不同的浏览器中自己尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM