问题:
When returning an object from an arrow function, it seems that it is necessary to use an extra set of {}
and a return
keyword because of an ambiguity in the grammar. 从箭头函数返回对象时,由于语法上的歧义,似乎有必要使用额外的{}
和return
关键字。
That means I can't write p => {foo: "bar"}
, but have to write p => { return {foo: "bar"}; }
这意味着我不能写p => {foo: "bar"}
,而必须写p => { return {foo: "bar"}; }
p => { return {foo: "bar"}; }
. p => { return {foo: "bar"}; }
。
If the arrow function returns anything other than an object, the {}
and return
are unnecessary, eg: p => "foo"
. 如果arrow函数返回的不是对象,则{}
和return
都是不必要的,例如: p => "foo"
。
p => {foo: "bar"}
returns undefined
. p => {foo: "bar"}
返回undefined
。
A modified p => {"foo": "bar"}
throws “ SyntaxError
: unexpected token: ' :
'” . 修改后的p => {"foo": "bar"}
抛出“ SyntaxError
:意外令牌:' :
'” 。
Is there something obvious I am missing? 有什么明显的我想念的东西吗?
解决方案:
参考一: https://stackoom.com/question/1wiUx/返回对象的ECMAScript-箭头函数参考二: https://oldbug.net/q/1wiUx/ECMAScript-6-arrow-function-that-returns-an-object
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/4406260