DEV Community

Discussion on: this keyword in JavaScript

Collapse
chinmaymhatre profile image
Chinmay Mhatre Author • Edited on

Thank you for the feedback! I will update some the things like ideal usage. However, I do not believe " just don't use this" is point of the blog . In high school, we were taught about the atomic model . In that chapter we started with the " John Dalton " model , after teaching us the model the professor told us " You know what we don't use this model anymore " and the professor went on to teach us the rest of the 4 models. Thus , I believe even though there are better ways to do something we do need to know how things work in general.

Of course there won't be any weirdness if you avoid it .😂
My job while writing this blog was to give out as many cases and outcome as I could .
I do believe I should give out cases where it should actually be used.

In ReactJS, again yes there are function components! But what if someone's job or project just uses class components, I don't think being subjective while writing an article is the way to go , saying " you know what you can use function component just use those instead of class components " isn't a way I would like to go about things.😂

I don't think if anyone googled " this keyword in JavaScript " they want to see " just don't use it " 😂.
Hence, "They won't do it" , "this won't happen" is subjectivity that I try to avoid.

Cheers!

Collapse
lukeshiru profile image
LUKESHIRU

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