Now I'm going to explain the this keyword. I know it's very confusing, and I still face many programmers who tell me that they did not completely understand the this keyword, despite the fact that they are no longer beginners. I remember back in the day, I extremely struggled to understand this keyword. And yes, I admitted, it's confusing. And after this tutorial, I promise I am going to make it super easy for you to understand it too. So first of all, what is this really is, this keyword actually refers to the object that is currently calling the function or the object that we are currently using.
So let's now take some examples and see what it means. So first of all, I want you to open an empty tab in your Google Chrome so that we are going to work in the console instead. So here I'm going to use, I'm going to open a new tab, and then I'm going to click on expecting inspect. And then I'm going to click on console. And I'm going to give myself a big space here. And now let's begin with an example.
Let's first just print the base keyword using the console log and see what it means. First of all, I'm going to use the console console dot log. And then I'm going to print the vis inside and see the result. Don't forget the semicolon, then hit enter. And as you can see, what you are going to get is the window object. As you can see, this is the window object.
And of course, it has many, many properties and functions. And what the window object means is that it's the window itself. The bigger or the biggest object in the browser. So always in JavaScript, the bigger object that you are going to work with if there is no object is the window object in any browser, or the global object in Sublime Text or any text editor. So if I get to Sublime Text, and use exactly the same if I use the console dot log, and just print this key word, what I'm going to get is that I'm going to get an empty object. But here it did not say global, but it's actually the global.
It's the global object. And why is that? Because there is no other object. Here. This refers to the global object. Always in JavaScript, there is a be the biggest object here.
And this object is the global object. As long as there is no other object, then this key word is will refer to the global object in sublime, or in the window object in Google Chrome or any browser. Now, if I get back to Google Chrome, now I'm going to close this window object because it has a lot of functions. And let's take other examples so that we understand what this actually refers to. Here, what what I'm going to tell you is that I'm going to create a simple object like we learned before. So for example, let foot baller equals to object.
And then it's going to take two parameters. The first one is name. And here, Ronaldo. And then the second property is a function, which is called speed, the speed of that player. And here it's a function and it's gonna take it's gonna take you Just it's going to just print console dot log, and it's just going to print this, I'm not going to create this PDF, I'm not going to create it, but let's just try this, this printing here and see what we get. And by the way, don't hit enter at all, because if you hit enter, it's gonna just execute incomplete function, you need to write everything in the same line here.
And once you finish just click, just hit enter. It's gonna just create this object. And now if I call football, or if I call the speed functions speed, and then parentheses, and then semicolon, don't forget the semicolon, and then hit enter. Guess what I'm going to get? What I'm going to get is that the object itself, I'm going to get the complete object of a football or object, because here I told this speed method to just print out the this keyword. So this keyword inside an object refers to the object itself.
So here it means this keyword, as you can see here refers to the complete object to this object, this complete object from here to hear from the, from the far left to the far right. So what this refers in an object to the object itself, so when whenever you use it inside an object, it refers to that object. But there is a third way that this refers to. So this also refers to the object when we use when we create an object from a constructor function. So let's try this but we are not going to try it here. Because here it will not we will not be able to create a big A B code.
Let's get back to Sublime Text. So here in Sublime Text, what I'm going to tell you is that I'm going to create it constructor function. And also I'm going to call it foot bowler. And this function is going to have, let's say it's going to have this on name. And as we know, as we learned in the when we create the construct when we created the constructor function, this now refers to the object that we are going to create from this constructor function. So it refers to this footballer.
So this name equals to, let's say, Ronaldo, and I am going here to just console just print out this from inside the constructor function this right and what I'm going to do here is I am going to create an object from this constructor function. So I'm going to create let throught baller one equals two. And to create an object from this constructor function, you type new and then footballer, as we learned, and that's it. If now I save and run, guess what I'm going to get, what I'm gonna get is that the footballer object, as you can see, it has one property now, because now when you created an object here from this constructor, this now refers to this object, because once you create a new footballer, it immediately creates an object. And this now refers to this object. So whenever you use this inside, inside this function, it refers to this object.
And even if I make this more complicated than this, if here suppose that I create another function in that inside this constructor function that says speed, and then equals to function and then parentheses curly braces and then console dot log. And I'm going to just log the this keyword and see what it refers to. Now if I call this function here, I would use the footballer object and then call this function which is going to which is going to print this keyword. What I'm going to get guess what I'm going to get. What I'm going to get is that I am again going to get the complete object because he or this refers to the object that we have created. So always remember that this keyword refers to the object that we are currently using or the or the object that is calling this function the function that we are calling.
So you need to trace back the object the object now is the football or object. So the football or object here the context is that this football or object, as this name has With speed, and now this refers to this object. But suppose now suppose now if I delete all of this, and create just a function without not an unconstructed function, just a simple function, let's say function, display name, display name. And this function, what it's going to do is just going to console, it's just going to display the this keyword. Now, if I call this function, display name, if I save and run, guess what I'm going to get what I'm going to get as you can see, what I am going to get is the global object, I am going to get the global object because the reason for that because now this function is a member of the global object.
It doesn't refer to any constructor function or it doesn't refer to any constructor function or any object, it's just a function of the global object. here behind the scenes. There is some global here, glow Bell object that calls this method, but it's not seen here you can see it. It's just, it's, it's available. It's there, but you can see it. It's like using the footballer when we use the footballer, we created new object, we created new, and then footballer, foot baller, and then we stored that into a variable, any variable, maybe let f equals to this and then we use this F, we use this F to call any function inside this football or so here, put ball right to call any any function inside this football.
So for example, the speed we can call the speed right here, here. Let me just comment these out. Here. What we did is the same But this function refers to the global object. It's a sign of the global object. So whenever you call this function, this keyword refers to the object of this function the object is the global object.
So to recap, this refers to either the window object, the window, in any browser, or the global, global in any text editor, or it refers to the constructor function or the object that we are going to create from the constructor function, the new and then the constructor function. Or it refers to the object that we used or we created, for example, var, a equals to the object, and then we use the this keyword. So this keyword always refers to the object that we are currently using there is always remember that there is always some object that you are using either the biggest object which is the global or the window object, or the object that you have created yourself by using just a simple object or an object from a constructor function. So hopefully now you understand the this keyword much, much better, and you can use them much easier.
And I recommend you that you practice these a lot, create your own function, or create your own functions, either just simple functions or constructor functions and try to use the this keyword and I'm going to also help you with that.