javascript return 和 return() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22988640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:08:54  来源:igfitidea点击:

What is the difference between return and return()?

javascript

提问by chris97ong

function a() { return 1; }
function b() { return(1); }

I tested the above code in Chrome's console, and both returned 1.

我在 Chrome 的控制台中测试了上面的代码,都返回了1.

function c() { return "1"; }
function d() { return("1"); }

I also tested the code above, and both of the functions returned "1".

我还测试了上面的代码,两个函数都返回了"1".

So what is the difference between using returnand return()?

那么 usingreturn和 有return()什么区别?

回答by RemcoGerlich

The same as between

之间一样

var i = 1 + 1;

and

var i = (1 + 1);

That is, nothing. The parentheses are allowed because they are allowed in any expression to influence evaluation order, but in your examples they're just superfluous.

也就是说,什么都没有。括号是允许的,因为它们可以在任何表达式中影响评估顺序,但在您的示例中,它们只是多余的。

returnis not a function, but a statement. It is syntactically similar to other simple control flow statements like breakand continuethat don't use parentheses either.

return不是函数,而是语句。这是语法类似于其他简单的控制流之类的语句break,并continue没有任何使用括号。

回答by alpartis

There is no difference.

没有区别。

returnis not a function call, but is a language statement. All you're doing with the parentheses is simply grouping your return value so it can be evaluated. For instance, you could write:

return不是函数调用,而是语言语句。您使用括号所做的只是对返回值进行分组,以便对其进行评估。例如,你可以写:

return (x == 0);

In this case, you return the value of the statement x == 0, which will return a boolean trueor falsedepending on the value of x.

在这种情况下,您x == 0将返回statement 的值,该值将返回一个布尔值truefalse取决于 的值x

回答by Zaheer Ahmed

Actually here precedence of ()is higher so it evaluate first:

实际上这里的优先级()更高,所以它首先评估:

Here firstly ("1")get evaluated, in following way:

这里首先("1")进行评估,方法如下:

("1")                     ==> "1"
("1","2")                 ==> "2"
("1","2","3")             ==> "3"
("1"+"2","2"+"2","3"+"2") ==> "32"
(2+3+6)                   ==>  11

so above statement is equivalent to:

所以上面的语句相当于:

return "1";

See visually:

视觉上看:

viusal

视觉的

So there is basically no difference in functionality but second one might be a negligibly bit slow as it firstly solve the brackets.

所以在功能上基本上没有区别,但第二个可能会有点慢,因为它首先解决了括号。

回答by Salvador Dali

There is absolutely no difference. If you will look at JS (ECMAScript) specification of returnstatement. Among many other things, it is telling you :

绝对没有区别。如果您将查看返回语句的JS (ECMAScript)规范。除了许多其他事情,它告诉你:

return [no LineTerminator here] Expression ;

返回 [此处没有 LineTerminator] 表达式;

that you can provide expression to return. Expression is hello, Math.abs(x), yourCustomFunc(7), or in your second case this can be 1or (1). Expression 1after evaluation is the same as (1)and the same as (((((1))))))or even as something really bizarre like (+(!(+(!1)))).

您可以向 提供表达式return。表达式是hello, Math.abs(x), yourCustomFunc(7),或者在您的第二种情况下,这可以是1(1)1求值后的表达式是一样的(1)和一样的,(((((1))))))甚至是非常奇怪的东西(+(!(+(!1))))

回答by IQAndreas

returnis a statementa keyword that starts the return statement, not a function.

return一个语句开始返回语句的关键字而不是一个函数。

As has been mentioned, the extra parentheses affect evaluation order, but are not used to "execute" the function named return. That is why these lines work without any problems:

如前所述,额外的括号会影响计算顺序,但不用于“执行”名为return. 这就是为什么这些行没有任何问题的原因:

return (1);
var a = (1);

They are, in effect, identical to these lines:

实际上,它们与以下几行相同:

return 1;
var a = 1;

The reason return()throws a syntax error is for the exact reason the following line throws an error(return statement included for comparison):

之所以return()抛出一个语法错误是的确切原因以下行引发错误(包括在比较中return语句):

return();    // SyntaxError: syntax error
var a = ();  // SyntaxError: syntax error

回答by Evan Knowles

There is no difference, the parenthesis are optional. See MSDN:

没有区别,括号是可选的。见MSDN

return[(][expression][)];

The optional expression argument is the value to be returned from the function. If omitted, the function does not return a value.

You use the return statement to stop execution of a function and return the value of expression. If expression is omitted, or no return statement is executed from within the function, the expression that called the current function is assigned the value undefined.

return[(][expression][)];

可选的表达式参数是要从函数返回的值。如果省略,则该函数不返回值。

您可以使用 return 语句停止函数的执行并返回表达式的值。如果省略了表达式,或者函数内没有执行 return 语句,则调用当前函数的表达式将被赋值为 undefined。

回答by hijarian

There is huge difference for humans, and zero difference for Javascript engine.

人类有巨大的差异,Javascript引擎的差异为零。

return 1is a statementdeclaring that we need to immediately exit the function yielding value of 1.

return 1是一个声明,宣布我们需要立即退出1功能产生价值。

return(1)is the same statement disguised as the function callby the idiotic convention that you are not obliged to insert space outside of parentheses in Javascript. If you would use code like this in production system, any maintainer will come to your office with stake and torches, after spending some time trying to decide whether you do really have return()function somewhere in codebase or just don't know what returnkeyword is for.

return(1)是根据愚蠢的约定伪装成函数调用的相同语句,即您没有义务在 Javascript 中的括号外插入空格。如果您要在生产系统中使用这样的代码,那么在花了一些时间试图确定您是否真的return()在代码库中的某个地方拥有功能或只是不知道return关键字是什么之后,任何维护人员都会带着股份和火炬来到您的办公室。

As many other people have already correctly said, parentheses do nothing except "group" with higher precedence the literal for the number 1.

正如许多其他人已经正确说过的那样,括号除了“组”之外没有任何作用,数字的文字优先级更高1

回答by Wolf

In the return statement, the parenthesesaround the expression are already built in.

在 return 语句中,表达式周围的括号已经内置。

In JavaScript, as in many other languages (like C, C++, Java, Python), the return statement has two parts: the keyword returnand an (optional) expression. So in, any case, all that is following the returnkeyword is firstevaluated as an expression, after that, the return statement is "executed" by passing the control back to the caller.

在 JavaScript 中,与许多其他语言(如 C、C++、Java、Python)一样,return 语句有两部分:关键字return和(可选)表达式。因此,在任何情况下,关键字后面的所有内容return首先作为表达式进行评估,然后通过将控制权传递回调用者来“执行”return 语句。

To use or not to use parentheses is a matter of style, whereas most style guides forbid them for trivial cases like the one quoted in your question, because it makes return falsely looking like a function.

使用或不使用括号是style 的问题,而大多数风格指南禁止在您的问题中引用的琐碎情况下使用括号,因为它使 return 错误地看起来像一个函数。

Later addendum

后来的附录

If with parentheses or without, never forget to place the optional expression behind the return, that is, in the same line. The real pitfallwith returnin JavaScript lies in adding a line break after it:

如果有括号或没有括号,永远不要忘记将可选表达式放在 后面return,即在同一行中。在真正的缺陷return在JavaScript在于加入后换行:

function test() {
  return 
  1;
}

... because above testfunction will return undefined.

...因为上面的test函数会返回undefined

回答by Rahul Singh Bhadhoriya

by adding parenthesis, we have made sure that javascript doesn't insert a semicolon before multiple statements written after return, for reference:- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion

通过添加括号,我们确保 javascript 不会在返回后写入的多个语句之前插入分号,以供参考:- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar #Automatic_分号_插入

example:

例子

return a + b;

return a + b;

is transformed to

转化为

return; a + b;

return; a + b;

by ASI.

由 ASI。

The console will warn "unreachable code after return statement". To avoid this problem (to prevent ASI), you could use parentheses:

控制台将警告“返回语句后无法访问代码”。为了避免这个问题(为了防止 ASI),你可以使用括号:

return ( a + b );
code copied from:- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

return ( a + b );
代码复制自:- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return