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?

Share Improve this question asked Jun 3, 2013 at 11:27 rwallacerwallace 33.7k44 gold badges134 silver badges281 bronze badges 3
  • 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 as x => x+1, but I don't know what the exact criterion for the difference is. – rwallace Commented Jun 3, 2013 at 11:36
Add a ment  | 

1 Answer 1

Reset to default 7

The 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