Stage 5: Jumping into JavaScript
What is 'this' in JS

In JavaScript, this refers to the object that the current function is a method of. The value of this depends on how the function is called, and it can be explicitly set using the call, apply, or bind methods.

Here are three ways this can be used in JavaScript:

  • In a method:
    const obj = {
      name: 'John',
      greet: function () {
        console.log(`Hello, my name is ${this.name}`);
      },
    };
    obj.greet(); // Hello, my name is John
    In this example, this refers to the object obj because the greet function is a method of obj.
  • In a constructor:
    function Person(name) {
      this.name = name;
      this.greet = function () {
        console.log(`Hello, my name is ${this.name}`);
      };
    }
    const john = new Person('John');
    john.greet(); // Hello, my name is John
    In this example, this refers to the new object that is being created when the Person constructor is called with new. The name and greet properties are added to the new object, and this is used to refer to the new object inside the constructor and the greet method.