What is this?
If you are new to JavaScript it is only a matter of time before you run into the concept of the this keyword. The this key...
For further actions, you may consider blocking this person and/or reporting abuse
I'd advise to avoid writing code that uses
this
altogether. It avoids issues with juniors misunderstanding and promotes less complex code. I don't think I've used it on purpose in the last handful of years.Not using this excludes you from a vast amount of logic. Modern JavaScript is centered around classes where it is necessary to use this in most cases. Maybe you have some technique I'm not aware of. I'd love to hear it if so!
Introducing less complexity is always great! I do think its an important concept of the language to understand though.
Things should be kept simple and obvious. Obvious code is great. You can change a feature years later without trying to understand it for a day and then fixing bugs, because you missed a few small details.
This is why "this" is a terrible feature in JS.
On the other hand JS has so many issues like this and that keeps away people, who try to write software without even finishing their first book and not having any idea what they do. So I call it even.
While the react community is moving towards functional components, most companies still use class based components. I guess you could just use anon functions in the class, but then you lose the ability to see the name while debugging, unless you do a bunch of extra code.
I don't write React code. I'm a full stack JS dev and avoid classes like the plague, when possible. Sure, every JS dev should understand the keyword, and I didn't mean to suggest otherwise. I just don't find many reasons to ever use a class beyond creating new Error classes or using a library that requires it. There are so many ways to accomplish similar things.
Also, it's not just the React community moving toward functional programming. I'd bet there are as many companies using functional techniques as classes. Ever use Array.map? Or lodash.filter? Promises and Observables are (basically) Monads. We got arrow functions and Array.prototype methods specifically because of the rise in FP in JS. I highly suggest Eric Elliot's series on Medium about FP, especially the "Forgotten History of OOP" medium.com/javascript-scene/the-fo...
I'm not a FP fanboy, either. It's just another tool that is useful sometimes. And if I find a place I need a class beyond what I said above, I would use it. I just can't think of many use cases where I need one.
Perhaps some code examples would help?
Definitely! I might add some if/when i have the time this week. Alternatively, if anyone wants a deeper dive with some great code examples I would suggest this post by Tyler McGinnis
tylermcginnis.com/this-keyword-cal...
Well written Angelika!
Thank you!
Great read!
Thanks. Informative.