The ‘this’ keyword is used to refer to the current object in a method or constructor. It can be used to access the object’s properties and methods.
For example,
class Car {
constructor(make, model, color) {
this.make = make;
this.model = model;
this.color = color;
}
getCarInfo() {
return `This car is a ${this.make} ${this.model} in the color ${this.color}.`;
}
}
const myCar = new Car(‘Honda’, ‘Civic’, ‘red’);
console.log(myCar.getCarInfo());
// Output: This car is a Honda Civic in the color red.