Skip to content

Latest commit

 

History

History
103 lines (87 loc) · 3.38 KB

File metadata and controls

103 lines (87 loc) · 3.38 KB

This

What is this?

In JavaScript, this is a special variable that’s created for every execution context (every function). It is a reference to the object that invoked the function it's used within.

Understanding this in Different Scenarios

  1. Standard Function Calls: In a regular function, this refers to the global object (in non-strict mode) or is undefined (in strict mode). Example:

    function test() {
      console.log(this);
    }
    test(); // logs: Window (or global in Node.js) in non-strict mode, undefined in strict mode
  2. As a Method in an Object: When a function is called as a method of an object, this refers to the object the method was called on. Example:

    let obj = {
      name: "John",
      sayHello: function() {
        console.log("Hello, " + this.name);
      }
    }
    obj.sayHello(); // logs: "Hello, John"
  3. In an Arrow Function: Arrow functions do not have their own this. The value of this inside an arrow function is always inherited from the enclosing scope. Example:

    let obj = {
      name: "John",
      sayHello: () => {
        console.log("Hello, " + this.name);
      }
    }
    obj.sayHello(); // logs: "Hello, undefined" because `this` is taken from outer scope (global or window object here)
  4. When using .call(), .apply(), and .bind(): The call(), apply(), and bind() methods allow us to call a function with a specific this value. Example:

    let obj1 = {name: "John"};
    let obj2 = {name: "Jane"};
    
    function sayHello() {
      console.log("Hello, " + this.name);
    }
    
    sayHello.call(obj1);  // logs: "Hello, John"
    sayHello.call(obj2);  // logs: "Hello, Jane"
  5. In Event Handlers: In event handlers, this refers to the element that received the event. Example:

    button.addEventListener('click', function() {
      console.log(this); // logs: the HTMLButtonElement object that was clicked
    });

Common Mistakes with this

  1. Loss of this in method passed as callback: When we pass a method without an object, we can lose the context of this. Example:

    setTimeout(obj.sayHello, 1000); // logs: "Hello, undefined", because `this` is not bound to `obj`
  2. Expecting this in an Arrow Function to refer to the object: As previously stated, arrow functions don't have their own this context. So, using this inside an arrow function expecting it to refer to the object can lead to unexpected results. Example:

    let obj = {
      name: "John",
      sayHello: () => {
        console.log("Hello, " + this.name);
      }
    }
    obj.sayHello(); // logs: "Hello, undefined"

Ways to Preserve this

  1. Using .bind(): We can use the .bind() method to bind the this value of the function to a specific object. Example:

    setTimeout(obj.sayHello.bind(obj), 1000); // logs: "Hello, John"
  2. Using Arrow Functions in Class Properties: In classes, defining methods as properties using arrow functions can help preserve the context of this. Example:

    class MyClass {
      name = "John";
      sayHello = () => {
        console.log("Hello, " + this.name);
      }
    }
    
    let myInstance = new MyClass();
    setTimeout(myInstance.sayHello, 1000); // logs: "Hello, John"