Should you use this in JavaScript?
Many people feel that this
is incredibly confusing. Because it’s so confusing, they argue that this
makes JavaScript complicated, and you should avoid them like a plague.
But does this
really make your JavaScript complicated? Should you avoid using this
?
Don’t avoid this
this
doesn’t make JavaScript complicated like what people have suggested. These people only feel that this
is complicated because they don’t fully understand it. Because they don’t understand this
, they dissuade people from using it under the pretext of complexity and confusion.
As I explained in a separate article, this
can take six possible values depending its context. Six is a relatively easy number to conquer, isn’t it?
If you can’t understand this
, you’ll likely find it difficult to understand JavaScript frameworks like React or Angular. You’ll find it hard to become fluent in JavaScript.
this
is a JavaScript fundamental. Don’t shy away from it. Give yourself the chance to understand it.
Learn this regardless of what people say
JavaScript is a language that supports two main programming styles – object oriented programming (OOP) and functional programming (FP).
Object oriented programming is a style that revolves around creating “objects”. It uses this
heavily to access properties and methods.
const Obj = function () {
return {
prop: 'yay',
method: function () {
// access prop in the same object
console.log(this.prop)
},
}
}
Functional programming is a style that revolves around performing actions through function calls. It doesn’t use this
at all.
const partialResult = someFunction(data)
const finalResult = anotherFunction(partialResult)
When viewed this way, whether to use this
becomes a bigger question of whether to use OOP or FP. Both approaches has their merits and are favored by many experts.
If you choose FP over OOP because you’re afraid of this
, you’re effectively condemning half of the JS population without even understanding why.
To make a conscious choice, you need to start by understanding wtf this
is.
Besides, OOP is so popular in JavaScript that you’ll definitely encounter instances of this
in libraries. If you don’t understand this
, you’ll likely have a hard time understanding the library. Examples of popular libraries that use this
include React and Mongoose.
When do you use this?
this
is crucial in OOP. So, when you use OOP, you definitely need to rely on this
. When you don’t write OOP, you don’t use this
.
With this constrain in place, you only need to know four of six possible contexts. Do you know what they are?
Have a guess :)
You might want to refer back to this article to help you figure it out.
Wrapping up
Learn this
.
Learn it even though others tell you it’s complicated. Learn it, because it sounds confusing. Learn it and give yourself the chance to fully understand what this
is.
Don’t shy away from this
. Don’t shy away from the chance to become a better developer.
Learn this
so you can use it effectively when you encounter it.