admin管理员组文章数量:1399778
In JavaScript, falling off the end of a function returns undefined
; if you want to return a value, you need to use an explicit return
statement.
At least this was hitherto the case, but it looks like ECMAScript 6 will at least sometimes allow the return
to be omitted.
In what circumstances will this be the case? Will it be related to the distinction between function
and =>
or is there some other criterion?
In JavaScript, falling off the end of a function returns undefined
; if you want to return a value, you need to use an explicit return
statement.
At least this was hitherto the case, but it looks like ECMAScript 6 will at least sometimes allow the return
to be omitted.
In what circumstances will this be the case? Will it be related to the distinction between function
and =>
or is there some other criterion?
-
1
The
new
keyword already works in previous ES versions :-) – Bergi Commented Jun 3, 2013 at 11:29 -
When will this change in ECMAScript 6 trickle down to javascript supported by modern browsers? Also, what do you mean
return
to be omitted? Can't return be omitted in current javascript: "In JavaScript, falling off the end of a function returns undefined"? Do you mean returning a value even if return is omitted? Please clarify. – Menelaos Commented Jun 3, 2013 at 11:30 -
@meewoK I mean the impression I get from what I've been able to find is that you can't write the successor function as
function (x) {x+1}
but you will be able to write it asx => x+1
, but I don't know what the exact criterion for the difference is. – rwallace Commented Jun 3, 2013 at 11:36
1 Answer
Reset to default 7The definitive material on this subject is the latest ES Harmony specification draft, and specifically the part derived from the arrow function syntax proposal. For convenience, an unofficial HTML version can be found here.
In short, this new syntax will allow the definition of functions much more concisely. The ES spec draft has all the detail, I 'll explain very roughly here.
The syntax is
ArrowParameters => ConciseBody
The ArrowParameters
part defines the arguments that the function takes, for example:
() // no arguments
arg // single argument (special convenience syntax)
(arg) // single argument
(arg1, arg2, argN) // multiple arguments
The ConciseBody
part defines the body of the function. This can be defined either as it has always been defined, e.g.
{ alert('Hello!'); return 42; }
or, in the special case where the function returns the result of evaluating a single expression, like this:
theExpression
If this sounds rather abstract, here's a concrete example. All of these function definitions would be identical under the current draft spec:
var inc = function(i) { return i + 1; }
var inc = i => i + 1;
var inc = (i) => i + 1;
var inc = i => { return i + 1; };
As an aside, this new syntax is exactly the same great syntax that C# uses to allow the definition of lambda functions.
本文标签: javascriptReturning a value without an explicit return statementStack Overflow
版权声明:本文标题:javascript - Returning a value without an explicit return statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744184534a2594228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论