DEV Community

Guddu Kumar
Guddu Kumar

Posted on

This keyword in JavaScript

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

  1. In an object method, this refers to the object.
  2. Alone, this refers to the global object.
  3. In a function, this refers to the global object.
  4. In a function, in strict mode, this is undefined.
  5. In an event, this refers to the element that received the event.
  6. 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

Enter fullscreen mode Exit fullscreen mode

this Alone

  • When used alone, this refers to the global object.
  • Because this is running in the global scope.
  • In a browser window the global object is [object Window]:
  • In strict mode, 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)