为什么在 JavaScript 中返回时使用括号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20824558/
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
Why use parentheses when returning in JavaScript?
提问by silent_coder
In the Restify framework code I found this function:
在 Restify 框架代码中,我发现了这个函数:
function queryParser(options) {
function parseQueryString(req, res, next) {
// Some code goes there
return (next());
}
return (parseQueryString);
}
Why would the author write return (next());
and return (parseQueryString);
? Does it need parentheses there and if so, why?
作者为什么要写return (next());
和return (parseQueryString);
?它是否需要括号,如果需要,为什么?
采纳答案by Darin Dimitrov
It doesn't need to be that way, but it's valid JavaScript code. Actually it's quite uncommon to see such syntax. I guess it's a personal preference of the author.
它不需要那样,但它是有效的 JavaScript 代码。实际上,这种语法并不常见。估计是作者个人喜好吧。
回答by Andru
Using parentheses when returning is necessary if you want to write your return statement over several lines.
如果您想将return 语句写成多行,则在返回时使用括号是必要的。
React.js
offers a useful example. In the return statement of the render
property in a component you usually want to spread the JSX you return over several lines for readability reasons, e.g.:
React.js
提供了一个有用的例子。在render
组件中属性的 return 语句中,出于可读性原因,您通常希望将返回的 JSX 分布在多行中,例如:
render: function() {
return (
<div className="foo">
<h1>Headline</h1>
<MyComponent data={this.state.data} />
</div>
);
}
Without parentheses it results in an error!
没有括号会导致错误!
More generally, not using parentheses when spreading a return statement over several lines will result in an error. The following example will execute properly:
更一般地,在将 return 语句分散到多行时不使用括号会导致错误。以下示例将正确执行:
var foo = function() {
var x = 3;
return (
x
+
1
);
};
console.log(foo());
Whereas the following (without the parentheses) will throw errors:
而以下(不带括号)会抛出错误:
var foo = function() {
var x = 3;
return
x
+
1
;
};
console.log(foo());
回答by geoyws
// Create a component named MessageComponent
var MessageComponent = React.createClass({
render: function() {
return (
<div>{this.props.message}</div>
);
}
});
NOTE Why do we need the parentheses around the return statement (line 3)? This is because of JavaScript's automatic semicolon insertion. Without the parentheses, JavaScript would ignore the following lines and return without a value. If the JSX starts on the same line as the return, then parentheses are not needed.
注意 为什么我们需要在 return 语句(第 3 行)周围加上括号?这是因为 JavaScript 的自动分号插入。如果没有括号,JavaScript 将忽略以下行并返回没有值。如果 JSX 与返回在同一行开始,则不需要括号。
Taken from here.
取自这里。
回答by Basyonic
Parenthesis are used for two purposes in a return statement.
在 return 语句中,括号有两个用途。
- To support multi-line expression as mentioned in @Andru Answer.
- To allow returning object in arrow function like the below:
- 支持@Andru Answer 中提到的多行表达式。
- 允许在箭头函数中返回对象,如下所示:
() => ({ name: 'Amanda' }) // Shorthand to return an object
() => ({ name: 'Amanda' }) // Shorthand to return an object
This is equivalent to
这相当于
() => {
return { name : 'Amanda' }
}
For more information, please check this article. https://medium.com/@leannezhang/curly-braces-versus-parenthesis-in-reactjs-4d3ffd33128f
有关更多信息,请查看这篇文章。 https://medium.com/@leannezhang/curly-braces-versus-parenthesis-in-reactjs-4d3ffd33128f
回答by Royi Namir
Why would the author write return (next()); ... ?
作者为什么要写return(next()); ……?
Regarding next()
:
关于next()
:
Probably because his function is something like this:
可能是因为他的功能是这样的:
function next()
{
var i=0;
return function (){
// Do something with that closured i....
}
}
Regarding (xxx);
:
关于(xxx);
:
It is unnecessary. Every minifier will remove it.
这是不必要的。每个压缩器都会删除它。
Example (uglifyJS):
示例(uglifyJS):
becomes:
变成:
回答by Brian Cryer
Just to add to what others have said.
只是为了补充其他人所说的。
Using brackets around the return value is valid JavaScript, but mostly a bad thing to do.
在返回值周围使用方括号是有效的 JavaScript,但通常是一件坏事。
Mostly bad because it doesn't add anything yet increases the size of the JavaScript which means that there is more to download to the browser. Yes most people have fast broadband connections, but don't lose sight of the fact that everything you put in the JavaScript file needs to be downloaded so avoid unnecessary code bloat. This probably doesn't matter if you use a tool to compact your code (minifier has already been mentioned), but not everyone does.
最糟糕的是,它没有添加任何内容,但会增加 JavaScript 的大小,这意味着有更多内容需要下载到浏览器。是的,大多数人都拥有快速的宽带连接,但不要忽视这样一个事实,即您放入 JavaScript 文件中的所有内容都需要下载,从而避免不必要的代码膨胀。如果您使用工具来压缩代码(已经提到过压缩器),这可能无关紧要,但并不是每个人都这样做。
Sometimes it might aid readability. Hard pressed to think of an example in this case, but if the use of brackets makes your JavaScript clearer to you as the developer and thus easier to maintain then use them - even if it goes against what I said about code bloat.
有时它可能有助于提高可读性。在这种情况下很难想出一个例子,但是如果使用方括号使您作为开发人员对您的 JavaScript 更清晰,从而更容易维护,那么请使用它们 - 即使这与我所说的代码膨胀相悖。
回答by Atieh
This may be old but the return ()
can be used in this way:
这可能是旧的,但return ()
可以以这种方式使用:
function parseQueryString(req, res, next) {
var id = req.param('id');
return (id ? "Foo" : "Bar");
}
Less code, easy to read :)
更少的代码,易于阅读:)