DEV Community

Discussion on: this keyword in JavaScript

lukeshiru profile image

My point mainly is that this kind of articles are great to deal with legacy, but not to create something from scratch.

You can go down the rabbit hole of trying to make this work as expected on your code, or you can just avoid it and be a happier dev.

I talked several times with folks having problems with this and the solution always was to just avoid it (classic XY problem). The thing is that generally is good to mention the solutions but also make clear that you should avoid it on your own code, and use it only if you're dealing with legacy that is hard to refactor.

In React the vast majority of projects using classes are on a migration path, or have tech-debt tickets related to turning old class components into functional ones, mainly because is kinda straightforward and has a better structure (no more this.method = this.method.bind(this) and all that jazz). One thing that is quite useful is to configure ESLint so it has rules to avoid using this in general. They can start as warnings and after a while turn them into linting errors :D

Thread Thread
chinmaymhatre profile image
Chinmay Mhatre Author • Edited on

My point still remains, the title of the blog is " this keyword in JavaScript " and not " why you shouldn't use this " or " why you should use functional components " the blog is meant to explain this as it is.

I think it always solidifies why we use arrow functions or functional components because it had me puzzled. Why use functional components? why does this cause so much confusion? I think those who seek those answers will find the blog useful . The way I write blog is by stumbling into things that I find confusing and I write about it thinking someone else would have the same doubt.

I think those who want to geek out on why things are the way they are would like to read the article. Those who want quick fixes won't and that's okay :)

Some comments have been hidden by the post's author - find out more