DEV Community

What is 'this' in JavaScript

Naveen Dinushka on October 04, 2019

What does 'this' return? Here's an object which uses this keyword const man = { name: "rick", adventure() { console.log(this);...
Collapse
 
johnkazer profile image
John Kazer

Kinda exactly the reason I don't use this. Context dependent behaviour is a nightmare I can do without. A functional style of JavaScript is able to avoid it.

Collapse
 
g1itcher profile image
G1itcher

I'm sure you already know this, but it's worth saying for any beginners that stumble upon this article.

It's easy to avoid issues with "this" by mandating arrow functions be used either to wrap the required function or just define the functions as arrow functions originally.

Or use bind()

With arrow functions and bind there's not much need to do the old

self = this

solution.

Collapse
 
naveenkolambage profile image
Naveen Dinushka

no worries! :)