Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is the in JavaScript

Understanding 'this' in JavaScript

Getting to Know 'this'

Allow me to introduce you to the concept of 'this' in JavaScript. It's a special keyword that's used in functions and objects. When we say 'this', we're referring to the object that the function is a part of. You can think of it like how you refer to yourself as "I" or "me". In the JavaScript world, an object refers to itself as 'this'.

let car = {
    brand: "Toyota",
    startEngine: function() {
        console.log(this.brand + " engine started.");
    }
};

car.startEngine(); // Output: "Toyota engine started."

In the example above, 'this' inside the 'startEngine' function refers to the 'car' object.

How 'this' Works in Different Scenarios

The value of 'this' can change depending on how a function is called. Let's see how 'this' behaves in different scenarios.

'this' in a Regular Function

In a regular function, 'this' refers to the global object. In a browser, the global object is window.

function checkThis() {
    console.log(this);
}

checkThis(); // Output: Window {...}

In this case, 'this' refers to the global window object because checkThis is invoked as a regular function.

'this' in a Method

When a function is part of an object, we call it a method. Inside the method, 'this' refers to the object it belongs to.

let person = {
    name: "John",
    greet: function() {
        console.log("Hello, " + this.name);
    }
};

person.greet(); // Output: "Hello, John"

Here, 'this' inside the greet method refers to the 'person' object.

'this' in a Constructor Function

A constructor function is a special type of function used to create objects of the same type. Inside a constructor function, 'this' refers to the new object being created.

function Car(brand) {
    this.brand = brand;
}

let myCar = new Car("Toyota");
console.log(myCar.brand); // Output: "Toyota"

In the Car constructor function, 'this' is used to assign the passed brand to the new car object.

The Call, Apply, and Bind Methods

JavaScript gives us three methods—call, apply, and bind—that allow us to directly control what 'this' refers to.

The call Method

The call method allows us to call a function with a specified 'this' value and arguments.

let person1 = {name: "John"};
let person2 = {name: "Jane"};

function greet(greeting) {
    console.log(greeting + ", " + this.name);
}

greet.call(person1, "Hello"); // Output: "Hello, John"
greet.call(person2, "Hi"); // Output: "Hi, Jane"

The apply Method

The apply method is similar to call, but it takes arguments as an array.

let numbers = [1, 2, 3, 4, 5];

function sum() {
    let total = 0;
    for(let i = 0; i < this.length; i++) {
        total += this[i];
    }
    return total;
}

console.log(sum.apply(numbers)); // Output: 15

The bind Method

The bind method returns a new function where 'this' is set to a certain value.

let person1 = {name: "John"};
let person2 = {name: "Jane"};

function greet() {
    console.log("Hello, " + this.name);
}

let greetPerson1 = greet.bind(person1);
let greetPerson2 = greet.bind(person2);

greetPerson1(); // Output: "Hello, John"
greetPerson2(); // Output: "Hello, Jane"

Conclusion

In the world of JavaScript, 'this' is a versatile keyword that can represent different things based on the context in which it's used. It's a little like an actor who plays different roles in different movies. The same actor might be a hero in one film, a villain in another, and a side character in a third. Just like how you need to pay attention to the movie to understand the actor's role, you need to pay attention to the context to understand what 'this' represents in your JavaScript code. I hope this tour of 'this' in JavaScript has been helpful. Remember, learning is a journey, and every step brings you closer to your destination. Keep coding!