简体   繁体   English

为什么返回的值应该与JavaScript中的return语句在同一行?

[英]Why does the value returned should be on the same line as the return statement in JavaScript?

The following doesn't work as I would expect it to: 以下不起作用,正如我期望的那样:

function test()
{
  // Returns undefined, even though I thought it would return 1
  return
  1;
}

Apparently, the value should be on the same line: return 1; 显然,该值应该在同一行上: return 1; . Why can I write things like 我为什么可以写这样的东西

// Assigns 1 to foo just fine
foo
=
1;

...but the return statement doesn't work the same way? ...但是return语句的工作方式不同吗?

It's explicitly part of the language spec. 这显然是语言规范的一部分。 If it were not, there would still be return issues: 如果不是这样,将仍然存在return问题:

if (something())  return
counter = counter + 1;

Without the semicolon insertion rule, that missing semicolon would trigger behavior that's (I'd argue) just as bizarre as what happens now with return statements split across a newline. 如果没有分号插入规则,那么缺少分号将触发与我现在发生的异常奇怪的行为( return语句在换行符之间分割)。

Javascript automatically inserts a semicolon after the "return" statement if the return expression is not on the same line. 如果return表达式不在同一行,则Javascript会在“ return”语句之后自动插入分号。

JavaScript has a mechanism that tries to correct faulty programs by automatically inserting semicolons. JavaScript具有通过自动插入分号来尝试纠正错误程序的机制。 Do not depend on this. 不要依赖于此。 It can mask more serious errors. 它可以掩盖更严重的错误。 It sometimes inserts semicolons in places where they are not welcome. 有时会在不受欢迎的地方插入分号。 Consider the consequences of semicolon insertion on the return statement. 考虑将分号插入return语句的后果。 If a return statement returns a value, that value expression must begin on the same line as the return: 如果return语句返回一个值,则该值表达式必须与return在同一行开始:

return
{
   status: true
};

This appears to return an object containing a status member. 这似乎返回包含状态成员的对象。 Unfortunately, semicolon insertion turns it into a statement that returns undefined. 不幸的是,分号插入将其变成返回未定义的语句。 There is no warning that semicolon insertion caused the misinterpretation of the program. 没有警告说分号的插入会导致程序的误解。 The problem can be avoided if the { is placed at the end of the previous line and not at the beginning of the next line: 如果将{放在上一行的末尾而不是下一行的开头,则可以避免该问题:

return {
   status: true
};

Quoted from this post , citing JavaScript: The Good Parts by Douglas Crockford. 引自这个帖子 ,理由是JavaScript的:由Douglas Crockford的好的部分。 Copyright 2008 Yahoo! 版权所有2008 Yahoo! Inc., 978-0-596-51774-8 . Inc.,978-0-596-51774-8

JavaScript will insert a semi-colon in certain cases to try to make an otherwise invalid program into a valid one. JavaScript在某些情况下会插入分号,以尝试将原本无效的程序制作为有效的程序。 In particular, a return statement is an example of what's called a "restricted production" -- you aren't allowed to have a line break after return before the value, so it gets transformed into return; 特别是, return语句是所谓的“受限生产”的一个示例-不允许在返回之后在该值之前进行换行,因此它将转换为return; and the following value becomes a separate statement. 并且以下值成为单独的语句。

In the case of 如果是

foo
=
1

this is not a restricted production, and furthermore neither = or 1 are illegal tokens in that position, so there's no attempt to insert a semi-colon at the end of the preceding line. 这不是受限制的生产,此外, =1都不是该位置上的非法令牌,因此没有尝试在前一行的末尾插入分号。

JavaScript uses semicolon insertion! JavaScript使用分号插入! For every line you forgot to close it with a semicolon JS will insert it automatically! 对于您忘了用分号JS封闭的每一行,它将自动插入! In this case: 在这种情况下:

return // auto semicolon insertion
{ // that's fine
    1; // yey here's one
} // ok, np
// bye bye

This means in JavaScript the use of { opening bracket is not, like in other languages, by developer's choice. 这意味着在JavaScript中, {开头括号的使用不同于其他语言,这是开发人员选择的。 You have to follow the rules: 您必须遵循以下规则:

return {
    1;
}

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

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