Hacker News new | ask | show | jobs
by truetuna 3079 days ago
Could you expand a bit on shorthand property syntax or provide a link to somewhere I could read more? I'm still doing a lot of .bind(this) in many places and I'm looking for an alternative.
1 comments

Isn't `something = () => {}` just a property declared as an anonymous function? Or is there more going on under the hood? At first glance this looks like a poor choice for debugging purposes.
The important thing is that the ES7 property initializer with the arrow syntax binds the function's "this" to the instance.
I understand that, but that’s not necessarily acceptable if it makes debugging a nightmare.

Apparently there is also a bind decorator, which I think is nicer than either of the other methods, because it is explicit but also clear, and without the verbosity.

well the alternatives I know of were to call .bind(this) all over the place, or wrap your handlers in a fat arrow wrapper function which is also an anonymous function as you say. I think this is the nicer of the options, and Vue.js is similar but you provide a hash of functions keyed by name, which is basically the same thing just different syntax. This nice thing about React is it doesn't matter. You can just write functional components if you don't want the es6 class stuff.