DEV Community

Satyendra Pandey
Satyendra Pandey

Posted on

Uses of `this` in javascript

The this keyword in JavaScript refers to the context in which a piece of code is executed. Its value changes depending on where and how it's used.


Key Points About this

  1. Dynamic: The value of this depends on the execution context (how and where the function is called).
  2. Context-Dependent: It can refer to different things such as an object, global object, or undefined.

Scenarios and Behavior of this

1. In Global Scope

In the global execution context:

  • In a browser: this refers to the window object.
  • In Node.js: this refers to an empty global object ({}) in modules.
console.log(this); // Browser: window, Node.js: {}
Enter fullscreen mode Exit fullscreen mode

2. Inside a Function

  • In non-strict mode, this refers to the global object (e.g., window in browsers).
  • In strict mode, this is undefined.
// Non-strict mode
function showThis() {
    console.log(this);
}
showThis(); // Browser: window, Node.js: global object

// Strict mode
"use strict";
function showThisStrict() {
    console.log(this);
}
showThisStrict(); // Output: undefined
Enter fullscreen mode Exit fullscreen mode

3. Inside a Method

When a function is a property of an object (a method), this refers to the object that owns the method.

const person = {
    name: "Alice",
    greet() {
        console.log(`Hello, my name is ${this.name}`);
    },
};

person.greet(); // Output: Hello, my name is Alice
Enter fullscreen mode Exit fullscreen mode

4. In an Arrow Function

Arrow functions don’t have their own this. Instead, they inherit this from their surrounding (lexical) scope.

const person = {
    name: "Bob",
    greet: () => {
        console.log(`Hello, my name is ${this.name}`);
    },
};

person.greet(); // Output: Hello, my name is undefined
// Because `this` in the arrow function refers to the outer global scope.
Enter fullscreen mode Exit fullscreen mode

To avoid issues, arrow functions are useful when you want to ensure this retains the context of the enclosing function.


5. Using this in a Constructor or Class

In a constructor or a class, this refers to the instance of the object being created.

class Person {
    constructor(name) {
        this.name = name;
    }
    greet() {
        console.log(`Hi, I'm ${this.name}`);
    }
}

const alice = new Person("Alice");
alice.greet(); // Output: Hi, I'm Alice
Enter fullscreen mode Exit fullscreen mode

6. In Event Handlers

In DOM event handlers, this refers to the element that triggered the event.

document.getElementById("myButton").addEventListener("click", function () {
    console.log(this); // Output: <button id="myButton">
});
Enter fullscreen mode Exit fullscreen mode

If you use an arrow function, this will refer to the surrounding scope, not the element.

document.getElementById("myButton").addEventListener("click", () => {
    console.log(this); // Output: window (or undefined in strict mode)
});
Enter fullscreen mode Exit fullscreen mode

Changing this with call, apply, and bind

You can manually set this using call, apply, or bind.

  • call: Calls a function and sets this to the first argument.
  • apply: Similar to call, but takes arguments as an array.
  • bind: Creates a new function with this bound to the specified value.
function greet(greeting) {
    console.log(`${greeting}, my name is ${this.name}`);
}

const person = { name: "Charlie" };

// Using call
greet.call(person, "Hello"); // Output: Hello, my name is Charlie

// Using apply
greet.apply(person, ["Hi"]); // Output: Hi, my name is Charlie

// Using bind
const boundGreet = greet.bind(person, "Hey");
boundGreet(); // Output: Hey, my name is Charlie
Enter fullscreen mode Exit fullscreen mode

Common Issues with this

  1. Losing Context: When passing methods as callbacks, this might lose context.
   const person = {
       name: "Dana",
       greet() {
           console.log(this.name);
       },
   };

   const greetFn = person.greet;
   greetFn(); // Output: undefined (global `this` in non-strict mode)
Enter fullscreen mode Exit fullscreen mode

Fix: Use .bind() to explicitly bind this to the correct context.

   const boundGreetFn = person.greet.bind(person);
   boundGreetFn(); // Output: Dana
Enter fullscreen mode Exit fullscreen mode
  1. Arrow Functions Misuse: Be careful not to overuse arrow functions, especially in object methods, where you may need a method’s own this.

Summary

  • this is a dynamic reference to the execution context of a function or block.
  • Global scope: this refers to the global object (window or global).
  • Inside a method: this refers to the object owning the method.
  • Arrow functions: this is lexically inherited (from the surrounding scope).
  • Classes/Constructors: this refers to the new object being created.
  • Manually changing this: Use call, apply, or bind.

Top comments (0)