There are a plethora of articles all over the web about what is
this is one of the most confusing topics about
There are many answers to the question “What is
this?” but from my point of
view, the answer is just one: “Technical debt,” and that’s because, for me,
anything written using
this, can and should be written without it.
this is an implicit value, meaning we do not define it like arguments,
variables, etc. Instead, it is determined automatically for us. We might think
for a second, “that’s great,” but we have to consider that depending on where we
are in our code,
this changes, so we can’t be 100% certain about its current
Now, let’s see how this looks like if we don’t use
It is almost the same amount of code, but now it is pretty clear what everything does because we are explicit about it.
Some posts out there present “easy solutions” to this kind of issue using some
methods that come with a function:
apply. To me, that’s
like putting patches over patches instead of fixing the root issue. For example,
let’s say that we have a function that takes a callback called
I already have a post about why we don’t need
classes to code, and this post is related, mainly because
this is extremely
common when working with
class. Some might even say that if we work with
static methods and stuff like that, then we don’t use
this, but my answer is:
Why are we even using
class then? We could have those static methods be
functions in a module and use them directly.
No, we need to know how this works, not to mention that there are many native
APIs in the browser and the server that uses classes, and learning how to deal
this is useful when working with them. However, we should avoid code that
this to improve our lives and the lives of other developers.
I wrote this article mainly because some folks are explaining what
this is and
trying to “sell it” like something good when it’s something awful. So, everyone
should learn how to deal with
this if we encounter one in the wild, but avoid
it as much as possible in the code we write.