4 ms·
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 Constructo
by pighive 8y ago
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.
- worldsayshi 8y agoNot sure if it answers your whole question but it means that the value of this.get gets assigned to all the other variables.
- pighive 8y agoMakes 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?
- dentemple 8y agoIt 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.
- dentemple 8y agoThe "=" 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.
- tachang 8y agoThe 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.