{"action":"created","issue":{"url":"https://api.github.com/repos/ecomfe/spec/issues/28","labels_url":"https://api.github.com/repos/ecomfe/spec/issues/28/labels{/name}","comments_url":"https://api.github.com/repos/ecomfe/spec/issues/28/comments","events_url":"https://api.github.com/repos/ecomfe/spec/issues/28/events","html_url":"https://github.com/ecomfe/spec/issues/28","id":120725723,"number":28,"title":"关于箭头函数只有一个参数的括号问题","user":{"login":"otakustay","id":639549,"avatar_url":"https://avatars.githubusercontent.com/u/639549?v=3","gravatar_id":"","url":"https://api.github.com/users/otakustay","html_url":"https://github.com/otakustay","followers_url":"https://api.github.com/users/otakustay/followers","following_url":"https://api.github.com/users/otakustay/following{/other_user}","gists_url":"https://api.github.com/users/otakustay/gists{/gist_id}","starred_url":"https://api.github.com/users/otakustay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/otakustay/subscriptions","organizations_url":"https://api.github.com/users/otakustay/orgs","repos_url":"https://api.github.com/users/otakustay/repos","events_url":"https://api.github.com/users/otakustay/events{/privacy}","received_events_url":"https://api.github.com/users/otakustay/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":28,"created_at":"2015-12-07T08:52:31Z","updated_at":"2015-12-10T09:20:08Z","closed_at":null,"body":"我们现在的规则是箭头函数只有一个参数，则此参数必须省略\r\n\r\n真实的情况是，只有一个参数的箭头函数会有不同场景不同状态，虽然没有明确的结论，但我想我们应该对各种情况再重新审视一下：\r\n\r\n### 普通的多行函数定义\r\n\r\n```js\r\nlet hash = str => {\r\n    let compute = require('md5');\r\n    return compute(str);\r\n};\r\n```\r\n\r\n### 普通的单行函数定义\r\n\r\n```js\r\nlet hash = str => require('md5').compute(str);\r\n```\r\n\r\n### 函数工厂\r\n\r\n```js\r\nlet hook = controller => route => console.log(route.url);\r\n\r\nlet hook = controller => route => action => console.log(action.context);\r\n\r\nlet hook = controller => route => action => {\r\n    let context = action.context;\r\n    console.log(context.url, context.args);\r\n};\r\n```\r\n\r\n### 用作参数的单语句函数\r\n\r\n```js\r\nlet list = names.map(name => name.split(' '));\r\n```\r\n\r\n### 用作参数的多语句函数\r\n\r\n```js\r\nlet list = names.map(name => {\r\n    let [firstName, lastName] = name.split(' ');\r\n    return {firstName, lastName};\r\n});\r\n```\r\n\r\n### 异步函数\r\n\r\n```js\r\nlet fetch = async id => request(`users/${id}`)\r\n\r\nlet fetchName = async id => {\r\n    let user = await request(`users/${id}`);\r\n    return user.fullName;\r\n};\r\n```\r\n\r\n顺便箭头+generator是早晚的事，不知道到时候会长啥样……讨论在这边：https://esdiscuss.org/topic/generator-arrow-functions\r\n\r\n### 数组解构\r\n\r\n```js\r\nlet getFullName = [first, last] => first + ' ' + last\r\n```\r\n\r\n### 对象解构\r\n\r\n```js\r\nlet getFullName = ({first, last}) => first + ' ' + last\r\n```\r\n\r\n解构我们现在是要求加括号的，主要原因是对象解构不加括号跑不了……\r\n\r\n另外解构也会和多行的组合，效果又有点不一样，可以写着感受下\r\n\r\n#### 参数展开\r\n\r\n```js\r\nlet warn = ...args => console.error(...args);\r\n\r\nlet filterAndJoin = ...args => {\r\n    return args.filter(s => !!s)\r\n        .map(s => s.replace(/ /g, ''))\r\n        .join(' ')\r\n};\r\n\r\n---\r\n\r\n这些情况还有可能是组合的，比如在参数中用异步函数等等……\r\n\r\n我个人在实践中的感受是，只有“用作参数的单语句函数”这一情况不加括号是另人比较舒服的，理由如下：\r\n\r\n1. 作为参数时，前后都已经有括号，使得这个表达式有明确的边界，容易识别\r\n2. 作为参数时是一个callback，其参数个数由当前正在调用的函数（如`.filter`或`.map`）决定的，出于接口的稳定性，这种callback的参数个数基本不会发生变化，所以不会有一会儿多了个参数要加括号了，一会儿参数又减掉了要把括号去掉了这种情况\r\n\r\n其它情况下，因为一些原因，我更倾向于加括号：\r\n\r\n1. 缺少边界的情况下，参数和函数体的识别变得困难，特别是在多级函数工厂再配上一个单语句函数体的情况下\r\n2. 由于解构和参数展开的存在，使得参数并不是一个简单的identifier，前面后面经常会有杂七杂八的东西，少个括号包起来会造成阅读上一定程度的麻烦（要脑内look ahead之类的）\r\n3. 因为`async`和未来的generator的存在，参数前面还可能出现不同的关键字等，又会造成这部分和参数之间也缺少足够明确的分隔\r\n\r\n所以我们是不是集众人之力，对不同情况做一下判断，再整合出一个标准来？"},"comment":{"url":"https://api.github.com/repos/ecomfe/spec/issues/comments/163541861","html_url":"https://github.com/ecomfe/spec/issues/28#issuecomment-163541861","issue_url":"https://api.github.com/repos/ecomfe/spec/issues/28","id":163541861,"user":{"login":"hax","id":159840,"avatar_url":"https://avatars.githubusercontent.com/u/159840?v=3","gravatar_id":"","url":"https://api.github.com/users/hax","html_url":"https://github.com/hax","followers_url":"https://api.github.com/users/hax/followers","following_url":"https://api.github.com/users/hax/following{/other_user}","gists_url":"https://api.github.com/users/hax/gists{/gist_id}","starred_url":"https://api.github.com/users/hax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hax/subscriptions","organizations_url":"https://api.github.com/users/hax/orgs","repos_url":"https://api.github.com/users/hax/repos","events_url":"https://api.github.com/users/hax/events{/privacy}","received_events_url":"https://api.github.com/users/hax/received_events","type":"User","site_admin":false},"created_at":"2015-12-10T09:20:08Z","updated_at":"2015-12-10T09:20:08Z","body":"@otakustay 所以`prefer-arrow-callback`对于有`this`且后面不跟bind的并不会报error。"}}