Javascript syntax []() and ()() [duplicate]

2019-07-03 23:02发布

问题:

This question already has an answer here:

  • How does this object method definition work without the “function” keyword? 2 answers

I'm having trouble understanding these javascript syntaxes. In the block of code below, on the second line. The square bracket is quickly followed by a round bracket or parentheses which I suspected is used to get arguments. I do not understand how this two is being chained to form an expression and what it means.

 export const recipeCount = createReducer(0, {
      [types.ADD_RECIPE](state, action){
        return state + 1;
      }
    });

Also on this line, the connect method takes in two arguments, (state) => {return {}} and mapDispatchToProps . Then it is quickly follwed by () with an argument. At first, i though it was some of object casting in java but that doesn't make sense.

export default connect((state) => {return {}}, mapDispatchToProps)(AppContainer);

The code executes fine and produces expected result. I just don't understand what is going on. Pls Help, would be glad to get answersre accompanied with links to pages i can read for better understanding. Thanks.

回答1:

Answers are in the comment to the question. Had to copy them out again, so i can mark the question as answered and close it.

"Not sure what's going on with the first one. For the second one, connect() is a function that returns a function so the second () is to immediately call that returned function." – Ouroborus

"The first one is a dynamic object literal property that is also an object method. I find this not readable at all. I would re-write that one. – Davin Tryon"

and also a link to Computed property names to make it clearer from – Denys Séguret

Thanks Guys.