返回对象的ECMAScript 6箭头函数

时间秒杀一切 提交于 2020-10-06 03:15:28

问题:

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!