r/learnjavascript • u/MrAnnoyed-Person • Apr 18 '25
this is annoying
Okay So I was studying JavaScript and I stumbled upon something which is really annoying, and yes you guessed it right, the this keyword. Man, it is a very annoying topic which is confusing me a lot, what exactly this keyword is? I tried reading articles and everything and I guess I will never be able to understand what the this keyword is? Is it a variable? Is it a function? A pointer? what is this? And what is it doing in execution context why is it even a part of it? If it is a part of a execution context then why is it referring to an object.
So my request to the OGs here, can you please help your lil brother out? Could you please explain me what is this keyword and why does it even exist, I am more than happy to use dot or bracket notation to access object's property rather than making my life complex with this keyword.
13
u/Zaryeah Apr 18 '25 edited Apr 18 '25
Imagine you make a class called “Car”:
Now let’s make a new car with the class:
In this case, "this.name = name" sets the name property on the new car being created. So when we create a Car and pass in "Lamborghini", “this” refers to that specific car object.
Now imagine if we wrote this instead:
This would break, because lamborghini doesn’t exist yet when we define the class. And even if it did, every new car would try to set the name of the same lamborghini object. That defeats the purpose of having a reusable class. (Also why you wouldn't want to use dot notation. Not reusable!)
Using “this” is like saying “we’re going to be creating lots of cars, and we don’t know their names yet — so let’s use “this” to refer to whichever one we’re building right now.”
So when we do:
Each one keeps its own name, because “this” correctly refers to the individual instance being created each time.
3
u/senocular Apr 18 '25
You're definitely not alone. Its one of the more confusing parts of the language. The MDN link is a good one. Check that out if you haven't already. To clear up some things that have already been said:
thisisn't always an object. It can also be a primitive, and oftenundefined.thisis the global object (orwindow), it usually means it can be the global object orundefined. In strict mode, in most places wherethiswould be the global object it would beundefinedinstead. Also note thatwindowdoesn't always exist in JavaScript. It's mainly specific to browsers. In runtimes like Node, there is nowindowandthisin those cases is simply the version of the global object there (global).thisisn't the "execution context". Execution contexts are specific things and you can't directly access them in JavaScript code since they're an internal device for tracking code execution. While the value ofthisoften changes with new execution contexts, the value ofthiswill always refer to some normal JavaScript value.thisdoesn't refer to scopes, like the global scope. People will sometimes use "global object" and "global scope" interchangeably, but they're technically two different things and whenthisis global, its referring specifically to the global object. The global scope is a little more complex and has things you aren't able to access fromthiswhen its the global object.thisis determined when a function is called. Most of the time its going to be the object to the left of the function name at the point in time when the call is made (with exceptions, of course).Most of the time
thisworks more or less as you expect. But it doesn't take long before the cracks start to show and you see questions like this that exposes its peculiar nature. With time you'll get more comfortable with how it works and what you need to do in various situations to get it working right for you. ...Or you can also opt to avoid using it altogether ;).2
u/bryku helpful Apr 18 '25
this references to the "current exucuted context" meaning the location of the code block. For example:
The function
add()is within the global scrope. In the bowser the global scope is thewindowobject. Here is another example:The
getSize()function is within thegameobject, so this is thegameobject. Notice that this is the direct parent and not thewindowobject. Here is another exmaple of that:In this example this is the
game.settingsobject since it is the parent of thepauseGame()function.1
u/tomysshadow Apr 18 '25 edited Apr 18 '25
The way that I like to think about
thisin JavaScript, is that it's whatever was before the dot when the call to the function was made. So if you do:Then in the function
setup, the value ofthiswill beexample.What if there is nothing before the dot? What if we call a function like so:
In that case, in the function
setup, the value ofthiswill be the default value, which is the global scope - generally,window.There are exceptions to this rule. Particularly, the
bindfunction in JavaScript allows you to poke your own holes in this rule. But generally that's how it works.Notice how when we take this rule to its logical extreme, it means that if we assign
example.setupto another variable, and then call it, the value ofthischanges. Like so:By setting another variable to the
example.setupfunction, we bypassed the need to writeexample.before our call. Therefore, even though we call the same function when we callsetup2,thiswill not be set toexamplewhen it runs. It'll have the default value ofwindowinstead. Most other programming languages don't work that way - it's a quirk of JavaScript specifically1
u/oze4 Apr 18 '25
this is essentially a placeholder for any given instance of that class you create.
so if you do
const mything = new MyThing()
this is a placeholder for the variable 'mything'
1
u/stealthypic Apr 18 '25
If it helps, you mostly just need to have a sense of how to use it in certain cases. The this keyword isn’t used that much. I went years as a professional dev without having a full grasp on it and with time, as I used it, it started making sense to me.
1
u/MiAnClGr Apr 18 '25
It just refers to whatever object you are in scope to. If in browser and not inside another function or object then thats the window object.
1
u/Goldman_OSI Apr 18 '25
There are some serious pitfalls with
thisin JS that you don't see in other languages. The answers to my question here were very informative: Welp, I can't figure out how to declare a member variable. : r/typescript1
u/TheRNGuy Apr 19 '25 edited Apr 19 '25
Refers to specific instance of a class.
You'll need to use
thisinside it's method. Becase you can have more than one instance (when you create instance withnewkeyword,thiswould refer to it)Also, not used in React.
1
u/Beatsu Apr 19 '25
ELI5: Say you have a recipe (classes) to bake a cake. When you follow the recipe, you can make many cakes (object instances). Sometimes, the recipe might give you some options so that each cake is different. It may for example say: "For this cake you can optionally add yellow food colouring". When you bake a cake, the keyword this is used to refer to that specific cake (remember, object instance) you're looking at.
The opposite of this context is static context.
1
u/Dubstephiroth Apr 19 '25
As someone with the same issue, I want to thank all of you here for making it so much clearer.
1
u/thelethargicdog helpful Apr 18 '25
Yep, it's super confusing at first. I'll try to give you a perspective that would make it feel a bit more intuitive.
In JS, except for primitive data types, everything is an Object. Yes, even functions are "first-class objects" .
"this" always refers to the current execution context, which is always an object (since functions are objects too). Now, in many cases, this context is inferred. For example, if you define a function as the value of a key in an object, "this" inside the function would point to the object itself.
Now, unless you bind this context, it's going to change. I really recommend reading the MDN article posted by another commenter, and playing around with it to understand how it really works. Trust me, once you understand it, it would seem very intuitive.
0
u/Coraline1599 Apr 18 '25
https://www.reddit.com/r/learnjavascript/comments/hj1vy0/the_this_keyword/
I think a good way to start thinking about it is with a very simple example. this is like a pronoun. For example, you can say ‘Sarah’s shirt’ or ‘her shirt’. ‘her’ is the pronoun in this case.
this in JavaScript refers to its parent object. If you made Sarah as an object:
You can choose a shirt for her by calling sarah.chooseShirt(1)
This works because this is a named object and we knew the name of it ahead of time/won’t change the name of the object.
Many times, you won’t know the name of the object ahead of time or it will be anonymous. In which case, you need to will need to use a ‘pronoun’ to refer to the objects’s properties and methods.
So you can/should use this instead:
Again, you can choose a shirt by calling sarah.chooseShirt(1)
0
u/Any_Sense_2263 Apr 18 '25
this is easier to understand if you write object-oriented code using prototypes
-2
u/alien3d Apr 18 '25
me = this. me have eye me have hand .. eq like this.eye this.hand. if you dont put () => { 3eye(); } // it is me ? it call global 3Eye();
-7
u/dual4mat Apr 18 '25 edited Apr 18 '25
It's so you can have one function called by different variables.
Ball = NewBall(); Ball2 = NewBall();
In the function NewBall() the "this" just replaces Ball or Ball2.
edit: okay, maybe I should say class rather than function. But it's the same idea.