3

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.

Andreyco
  • 22,476
  • 5
  • 61
  • 65
Badmus Taofeeq
  • 761
  • 9
  • 18
  • 2
    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 Dec 30 '16 at 16:55
  • 7
    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 Dec 30 '16 at 16:57
  • That make sense. Thank you. – Badmus Taofeeq Dec 30 '16 at 17:01
  • @BadmusTaofeeq If it's clear for you, I'd suggest you close that question. – Denys Séguret Dec 30 '16 at 17:01
  • 1
    To make it totally clear, here's a [documentation on Computed property names](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names) – Denys Séguret Dec 30 '16 at 17:02
  • 1
    This is still hideous. I can't believe that would in any way pass a code review. – Jared Smith Dec 30 '16 at 17:21
  • 1
    @Gothdo That is not a good dup. It covers the shorthand object method, but not the computed property key aspect, nor the `()()` question. –  Dec 30 '16 at 17:39
  • @torazaburo This question is too broad anyway. – Michał Perłakowski Dec 30 '16 at 17:40
  • 1
    @JaredSmith I'm sure this hideous code would pass code reviews where it was written. this `[types.ADD_RECIPE]` smells like a committee designed factories and type hierarchies with a lot of verbose abstractions in between... – Denys Séguret Dec 30 '16 at 17:56
  • @Gothdo respectfully disagree: there are several as yet uncommon sugars in play here so its not a dupe of questions about any specific one, but it can be answered decisively in a one paragraph answer so its not too broad. I'm not saying its a great question, but I've seen plenty worse ones that are also not technically dupes. – Jared Smith Dec 30 '16 at 18:03
  • @DenysSéguret agreed: some UMLWeenie got their smudgy fingerprints all over this. – Jared Smith Dec 30 '16 at 18:03
  • @JaredSmith A question should ask one question; otherwise it's too broad. See http://meta.stackexchange.com/q/222735/310998 and http://meta.stackexchange.com/q/39223/310998 – Michał Perłakowski Dec 30 '16 at 20:32

1 Answers1

2

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.

Badmus Taofeeq
  • 761
  • 9
  • 18
  • 2
    Could I suggest that you re-write the comments into a consistent answer .. it would add a lot of value – Soren Dec 30 '16 at 17:35
  • Doubtful that we need an answer at all, since the individual questions are all well answered in other questions. –  Dec 30 '16 at 18:44