News for hackers. Stuff that matters.
Re:REST and GraphQL really aren't that different by pighive on Wednesday January 30, 2019 @01:53PM (#19038015)
I haven't done much JS, but what does this mean "this.post = this.put = this.delete = this.patch = this.query = this.mutation = this.get;" in Router Constructor.
Re:REST and GraphQL really aren't that different by dentemple on Wednesday January 30, 2019 @02:02PM (#19038125)
The "=" operator always returns the value on the right side, and it always processes the right side before the left side.

Therefore:

1 - this.mutation = this.get // returns this.get

2 - this.query = <return value of this.mutation = this.get, which is this.get>

3 - this.patch = <return value of this.query = this.mutation = this.get, which is this.get>

n - and so on...

Essentially, it's setting 6 different properties all to the same value.

---

EDIT: Here's the same concept, but in a different context...

function fibonacci(num, memo = {}) {

    if (num <= 1) return 1;

    if (memo[num]) return memo[num];

    return memo[num] = fibonacci(num - 1, memo) + fibonacci(num - 2, memo);
}

On the return line, the right-to-left property of "=" forces the extreme right to be processed first. It returns the recursive call.

Then, the return value of this recursive call gets saved to "memo[num]".

Finally, the return value of "memo[num] = fibonacci(...) + fibonacci(...)", which is still the same return value from the recursive call, gets saved to the function return.

Re:REST and GraphQL really aren't that different by pighive on Wednesday January 30, 2019 @02:47PM (#19038645)
Thanks for taking time to put an example. I am familiar with the assignment rule, but this is a refresher. My question was more on why it was one function being used for all others.
Re:REST and GraphQL really aren't that different by dentemple on Wednesday January 30, 2019 @08:37PM (#19041472)
Ah, well, could be useful for someone else who looks at it.
Re:REST and GraphQL really aren't that different by tachang on Wednesday January 30, 2019 @03:22PM (#19039031)
The reason you'd do this is because you are probably planning to return the same response for all the HTTP verbs.

Like if you wanted to return a empty hash {} for:

  GET /
  DELETE /
  PUT /
  PATCH /
  POST /
I guess just being lazy.
Re:REST and GraphQL really aren't that different by worldsayshi on Wednesday January 30, 2019 @01:57PM (#19038066)
Not sure if it answers your whole question but it means that the value of this.get gets assigned to all the other variables.
Re:REST and GraphQL really aren't that different by pighive on Wednesday January 30, 2019 @02:03PM (#19038139)
Makes sense, but why do we have to do it this way, assigning just get func to all other verbs (functions), cos this is an example?
Re:REST and GraphQL really aren't that different by dentemple on Wednesday January 30, 2019 @02:13PM (#19038221)
It turns six lines into one line.

It looks impressive in software interviews, but I don't really see the use in it for production code. Not every JS dev immediately knows what's going on with this syntax.