Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
731 views
in Technique[技术] by (71.8m points)

javascript - What do parenthesis surrounding brackets in the return statement of an ES6 arrow function do?

For example in redux actions, I've seen in someone's code:

export const updateMessage = text => {
   return (dispatch) => {
     dispatch(updateChatMessage(text))
   }
}

and:

const updateChatMessage = text => ({
   type: types.someActionType,
   text
})

it seems to function as an imply a return but I thought that was already implied in an arrow functions brackets following the fat arrow.

What do the parenthesis ({...}) do? are they necessary? Is there an alternate way to accomplish the same thing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

when you write myFunction = value => ({prop: value}) it return the object {prop: value}, in this case {} are object delimiter and not 'function delimiter'

const updateChatMessage = text => ({
   type: types.someActionType,
   text
})

another eg :

when you want to multiply by two each elem of an array you can write :

array.map(elem => {return elem * 2})

or

array.map(elem => elem * 2) //same result

and if you want an eg with () that wrap an object litteral :

let array = [{val: 2},
             {val: 4},
             {val: 8},
             {val: 16}];
             
let output = array.map( ({val}) => ({val: val*2}) );

console.log(output);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...