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 thi…


This content originally appeared on Zell Liew and was authored by Zell Liew

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.


This content originally appeared on Zell Liew and was authored by Zell Liew


Print Share Comment Cite Upload Translate Updates
APA

Zell Liew | Sciencx (2017-08-23T00:00:00+00:00) Should you use this in JavaScript?. Retrieved from https://www.scien.cx/2017/08/23/should-you-use-this-in-javascript/

MLA
" » Should you use this in JavaScript?." Zell Liew | Sciencx - Wednesday August 23, 2017, https://www.scien.cx/2017/08/23/should-you-use-this-in-javascript/
HARVARD
Zell Liew | Sciencx Wednesday August 23, 2017 » Should you use this in JavaScript?., viewed ,<https://www.scien.cx/2017/08/23/should-you-use-this-in-javascript/>
VANCOUVER
Zell Liew | Sciencx - » Should you use this in JavaScript?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2017/08/23/should-you-use-this-in-javascript/
CHICAGO
" » Should you use this in JavaScript?." Zell Liew | Sciencx - Accessed . https://www.scien.cx/2017/08/23/should-you-use-this-in-javascript/
IEEE
" » Should you use this in JavaScript?." Zell Liew | Sciencx [Online]. Available: https://www.scien.cx/2017/08/23/should-you-use-this-in-javascript/. [Accessed: ]
rf:citation
» Should you use this in JavaScript? | Zell Liew | Sciencx | https://www.scien.cx/2017/08/23/should-you-use-this-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.