More About ‘this’ Keyword in JavaScript
Published: 2022-06-07 17:44:33

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cf7d8f4qylyg3zoy7iux.png)
Whenever any code is executed in JavaScript, it runs inside an Execution Context. The Global Execution Context is the very first Execution Context on the the call stack. The first thing JS Engine does is, it creates Global Execution Context and gives you two things:

1. Global Object
2. ‘this’ keyword 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p595r8990nnb5a8n8sl8.png)
Suppose if we create a user object with some properties and methods,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y9m0y2y5nekfjpq4zcnr.png)
on calling the login function , we can see that the this keyword is nothing but user object!
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o67o5p7hmm02wymew46j.png)
Now suppose we attach any new method on user, and call it ,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdlk0is46b2r3rn9n95s.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7rqkvufcfqah2im2xyin.png)
hen also the this keyword points to user object!

Thus , the ‘this’ keyword is nothing but the object that the function is a property of!
Now suppose we have a regular function ,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ue1n590xli4l5ajacbcp.png)
on calling the function we can see that the this keyword points to global object.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dg20obdkv9l0284eatti.png)
But that’s not the case with constructor function.

Lets see this constructor function f2,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1upqx0qw7ai35be3657a.png)
Now if create an object of f2 and log it to the console we get,![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hyj0vyf8w3vqn7nvext.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/928hry15llpxgfq3lmhv.png)
Thus we can say that , when dealing with regular functions ,the ‘this’ keyword points to the global object. But in case of constructor function ,it will point to that respective object itself.
Now see the below object obj1,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0a7wmrm4gp1idpgtnck.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pljbsyujgt8stgo064u2.png)
since the ‘anotherfunction’ is not really associated with obj1(it behaves like a regular function)directly, the ‘this’ keyword inside it points to global object.

so in this case , how can we make that ‘this’ ,point to obj1?

Well this can be achieved by arrow functions.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfsf32bmxtnltu47l934.png)
Before arrow function, this was done by using bind function,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9nyrrhrrflb565sickx.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rvh59bp6w3mmyl0wmtn.png)
This was all about ‘this’ keyword!!!

Hope it helps!

Happy learning!