This keyword in JavaScript
Published: 2022-08-31 09:47:25


In JavaScript, <u>the this</u> keyword refers to an **object**. 
Which object depends on how <u>this</u> is being invoked (used or called). 
The <u>this</u> keyword refers to different objects depending on how it is used: 

1. In an object method, this refers to the object. 
3. Alone, this refers to the global object. 
5. In a function, this refers to the global object. 
7. In a function, in strict mode, this is undefined. 
9. In an event, this refers to the element that received the event. 
11. Methods like call(), apply(), and bind() can refer this to any object. 

 

**this in a Method**

- When used in an object method, this refers to the object. 
- In the example on below, this refers to the person object. 
- Because the fullName method is a method of the person object. 

```

 const person = {
    firstName: "John",
    lastName: "Doe",
    id: 5566,
    fullName : function() {
      return this.firstName + " " + this.lastName;
    }
};
console.log(person.fullName())//John Doe

```

 

**this Alone**

- When used alone, <u>this</u> refers to the global object. 
- Because <u>this</u> is running in the global scope. 
- In a browser window the global object is _[object Window]_: 
- In <u>strict mode</u>, when used alone, this also refers to the global object: 

 ```
 let x=this;
console.log(x)//window
console.log(this)//window
console.log(window==this)//true
```

 

 
**this in a Function (Default)** 

 

- In a function, the global object is the default binding for this. 

- In a browser window the global object is [object Window]: 

- In regular function and arrow function this refers to window object. 
 

```
function a(){
    return this;
  }
  console.log(a())//window

  let b=()=>{
    return this;
  }
  console.log(b())//window
```

 


**this in a Function (Strict)**

 

JavaScript strict mode does not allow default binding. 
So, when used in a function, in strict mode, this is undefined. 


```
'use strict'
  function a(){
    return this;
  }
  console.log(a())//undefined
```
 

 

**Explicit Function Binding**

The _call()_ and _apply()_ methods are predefined JavaScript methods. 

They can both be used to call an object method with another object as argument. 

The example below calls person1.fullName with person2 as an argument, this refers to person2, even if fullName is a method of person1: 

```

 const person1 = {
    firstName:"Manish",
    lastName:"Kumar",
    fullName: function() {
      return this.firstName + " " + this.lastName;
    }
  }
 
  const person2 = {
    firstName:"John",
    lastName: "Doe",
  }
 
console.log(person1.fullName.call(person3))//John Doe
```

**Function Borrowing**
 

With the _bind()_ method, an object can borrow a method from another object. 

This example creates 2 objects (person and member). 

The member object borrows the fullname method from the person object: 

```
 const person11 = {
    firstName:"John",
    lastName: "Doe",
    fullName: function () {
      return this.firstName + " " + this.lastName;
    }
  }
 
  const member = {
    firstName:"Hege",
    lastName: "Nilsen",
  }
 
  let fullName = person11.fullName.bind(member);
  console.log(fullName())//Hege Nilsen
```

 
If you like it, please share it.....