发表文章

[Javascript] 箭头表达式中的括号。 Parentheses in arrow expressions.[javascript-styleguide]

piotrsiatkowski 2017-10-9 36

另一件事, 我认为我们可能并不都同意在箭头表达式中使用括号。

为一致性, 这是一个很好的事情, 使用他们所有的时间, 但在我看来, 短期转换像

list.map(x=>+1);

我们应该坚持上面的形式。没有返回表达式, 没有分号, 没有括号。

但承诺呢?承诺的回调函数通常是复杂的, 但预期的参数是很好的定义的 (它承诺的解决价值)。缺少括号是在这种情况下, 友好的眼睛和更可读我的意见。你觉得怎么样?

原文:

Another thing that I think we may not all agree on is parentheses usage in arrow expressions.

For consistency, it is a good thing to use them all the time, but in my opinion for short transformations like

list.map(x => x + 1);

we should stick to the form above. No return expression, no semicolon, no parentheses.

But what about promises? Promise's callback function is usually complicated and long but the anticipated parameter is well defined (it promise's resolved value). Lack of parentheses is in this case friendly for eyes and more readable in my opinion. What do you think?

相关推荐
最新评论 (12)
CERios 2017-10-9
1

我不同意。

在编写代码时, 我认为我们应该尽量做到尽可能的清楚, 如果输入更多是一个结果, 那么就这样吧。

// Don't mind this
list.map((x) => x + 1);

// Prefer this
list.map((x) => {
    return x + 1;
});

就我两美分总是乐意与多数人一起去。

原文:

I disagree.

When writing code, I think we should try to always be as clear as possible and if typing more is a consequence of that, so be it.

// Don't mind this
list.map((x) => x + 1);

// Prefer this
list.map((x) => {
    return x + 1;
});

Just my two cents. As always happy to go with the majority.

jthomas1 2017-10-9
2

我只是养成了不使用它们的习惯, 因为你不需要和它的眼睛更容易。然而, 如果有一个明显的负面后果, 不使用它们, 那么我很高兴去与它, 但现在我将继续飘扬的旗帜没有括号:P

原文:

I just got into a habit of not using them because you don't have to and its easier on the eyes. However if there is a demonstrable negative consequence of not using them then I'm happy to go with it but for now I will continue to fly the flag for no parens :P

piotrsiatkowski 2017-10-9
3

我能想到的唯一缺点就是一致性。我们也应该在链接功能上做一个风格。

我们想要这个吗?

elements 
    .map(x=>+1)
    .filter(x=>>2)
    .reduce(etc...

还是这个?

elements 
    .map((x=> {
        return+1;
    })
    .filter((x=>{
        return>2;
    })
   .etc
原文:

The only disadvantage I can think of is consistency. We should work out style on chaining functions too.

Do we want this?

elements 
    .map(x => x + 1)
    .filter(x => x > 2)
    .reduce(etc...

Or this?

elements 
    .map((x) => {
        return x + 1;
    })
    .filter((x) =>{
        return x > 2;
    })
   .etc
CERios 2017-10-9
4

我看到你的观点@PiotrSiatkowski -当使用这种函数方法时, 显式返回确实开始显得有点难看。对我来说, 总是, 总是括号虽然。眼睛容易;)

原文:

I see your point @PiotrSiatkowski - Explicit returns do start to look a bit ugly when using that kind of functional approach. For me, always, always parentheses though. It's easier on the eyes ;)

flaviotulino 2017-10-9
5

对于一行语句, 我不使用括号, 如@PiotrSiatkowski的第一个示例。
用于多行、括号和 return 语句。
这只是我通常遵循的风格, 你觉得怎么样?

原文:

For one line statement I use no parens, like @PiotrSiatkowski 's first example.
For multiple lines, brackets and return statement.
It's just the style I usually follow, what do you think?

jthomas1 2017-10-9
6

我很高兴去与没有括号1班轮和括号的一切, 如果这是共识。这就是 airbnb 已经说的。

原文:

I'm happy to go with the no parens for 1 liners and parens for everything else if that is the consensus. Which is what airbnb are saying already.

CERios 2017-10-9
7

民主已经说过了。我已经添加了规则。

"arrow-parens": [2, "as-needed", { "requireForBlockBody": true }]

公关为您的参考: BookingBug/bookingbug-angular#952

原文:

Democracy has spoken. I've added the rule.

"arrow-parens": [2, "as-needed", { "requireForBlockBody": true }]

PR for your ref: BookingBug/bookingbug-angular#952

CERios 2017-10-9
8

喜欢在一个衬垫内有括号, 一个参数。如:

elements 
    .map((x=>+1)
    .filter((x=>>2)
原文:

Prefer to have parentheses in one liners, with one argument. eg:

elements 
    .map((x) => x + 1)
    .filter((x) => x > 2)
CERios 2017-10-9
9

喜欢在一个衬垫没有括号, 一个参数。如:

elements 
    .map(x=>+1)
    .filter(x=>>2)
原文:

Prefer to have no parentheses in one liners, with one argument. eg:

elements 
    .map(x => x + 1)
    .filter(x => x > 2)
jthomas1 2017-10-9
10

喜欢在任何一个参数函数上没有括号, 例如:

promise.then(stuff=> {
    console.log(stuff)
})
原文:

Prefer to have no parentheses on any single argument functions eg:

promise.then(stuff => {
    console.log(stuff)
})
bogdansavin 2017-10-9
11

我使用:

元素
. 地图 (x = > x + 1)
. 过滤器 (x = > x > 2)
. 减少 (等等。

但它不是一个很大的处理 () {}

原文:

I use:

elements
.map(x => x + 1)
.filter(x => x > 2)
.reduce(etc...

but it's not a big deal with () { }

CERios 2017-10-9
12

感谢参加投票的人已经解决了

喜欢没有括号在一个划线员, 与一个论据赢取例如:

elements 
    .map(x=>+1)
    .filter(x=>>2)
原文:

Thanks for voting, those that took part. It's settled.

Prefer to have no parentheses in one liners, with one argument wins eg:

elements 
    .map(x => x + 1)
    .filter(x => x > 2)
返回
发表文章
piotrsiatkowski
文章数
1
评论数
4
注册排名
60887