Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single-Line to Multi-Line ES6 Fat Arrow Function?

I'm learning ES6 fat arrow functions. What is the correct way to change this code, so as to be able to put another line, even const a = 100; in the place indicated so that I can add some more lines to this function?

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

Update:

Here's ES6 code that runs:

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.

like image 228
VikR Avatar asked Jan 13 '17 03:01

VikR


People also ask

What does => mean in ES6?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

When you should not use arrow functions in ES6?

An arrow function doesn't have its own this value and the arguments object. Therefore, you should not use it as an event handler, a method of an object literal, a prototype method, or when you have a function that uses the arguments object.

Are arrow functions ES6?

Introduction. The 2015 edition of the ECMAScript specification (ES6) added arrow function expressions to the JavaScript language. Arrow functions are a new way to write anonymous function expressions, and are similar to lambda functions in some other programming languages, such as Python.

What is the difference between a normal function and an ES6 arrow function?

Since regular functions are constructible, they can be called using the new keyword. However, the arrow functions are only callable and not constructible, i.e arrow functions can never be used as constructor functions. Hence, they can never be invoked with the new keyword.


1 Answers

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:

{   filter: appointment => {     // ... add your other lines here     return true;   } } 
like image 177
nem035 Avatar answered Oct 02 '22 23:10

nem035