简体   繁体   English

单线转多线ES6胖箭Function?

[英]Single-Line to Multi-Line ES6 Fat Arrow Function?

I'm learning ES6 fat arrow functions.我正在学习 ES6 粗箭头函数。 What is the correct way to change this code, so as to be able to put another line, even const a = 100;更改此代码的正确方法是什么,以便能够放置另一行,甚至是const a = 100; in the place indicated so that I can add some more lines to this function?在指定的地方,以便我可以向这个 function 添加更多行?

IMAdded: (options, args) => ({
    IMAdded: {
        filter: newIM => true, *need to add another line here*
    },
}),

Update:更新:

Here's ES6 code that runs:这是运行的 ES6 代码:

const subscriptionManager = new SubscriptionManager({
    schema,
    pubsub,
    setupFunctions: {
        APPTAdded: (options, args) => ({
            APPTAdded: {
                filter: appointment => (true),
            },
        }),
});

I'd like to add some more lines into the code that returns true .我想在返回true的代码中添加更多行。

If you want to convert the following method into having more lines: 如果要将以下方法转换为包含更多行:

{
  filter: appointment => true
}

You have to add curly braces and a return statement: 你必须添加花括号和一个return语句:

{
  filter: appointment => {
    // ... add your other lines here
    return true;
  }
}
filter: appointment => true,
...

is (and parentheses aren't needed around true ) a shortcut for 是(并且括号不需要为true )一个快捷方式

filter: appointment => {
  return true;
},
...

which is a shortcut for 这是一个快捷方式

filter: function (appointment) {
  return true;
}.bind(this),
...

Any amount of lines can be added before return statement. 可以在return语句之前添加任意数量的行。

No braces or return needed if your function body is an expression which can span multiple lines (a ConciseBody in the spec)如果您的 function 正文是一个可以跨越多行的表达式(规范中的ConciseBody ),则不需要大括号或return

{
  filter: appointment =>
    true &&
    false
}

Note: I was initially confused about the question (hence the EDIT), and thought it was about linebreaks in arrow functions in general .注意:我最初对这个问题感到困惑(因此编辑),并认为它是关于一般箭头函数中的换行符 And, yes, they are allowed, but only after the arrow.而且,是的,它们是允许的,但只能箭头之后。
See also the ESLint rule implicit-arrow-linebreak , which lists this variant under "Correct code" (if configured "with the below option")另请参阅 ESLint 规则implicit-arrow-linebreak ,它在“正确代码”下列出了此变体(如果配置为“使用below选项”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM