What does this do inside a class?
1. What Is this in a Class
this inside a class is a reference to the current instance (object) created with new.
Example:
class User {
constructor(name) {
this.name = name; // this points to the object being created
}
greet() {
console.log(`Hi, ${this.name}!`);
}
}
const u = new User("Alice");
u.greet(); // Hi, Alice!Here:
- At the moment
new User("Alice")runs, a new object is created. thisinside the constructor points to exactly that new object.this.name = namewrites a property onto the instance.- Then
u.greet()calls the method, wherethis === u.
2. this in Class Methods
Class methods have no this of their own, it is determined at call time.
class User {
constructor(name) {
this.name = name;
}
sayHi() {
console.log(this.name);
}
}
const user = new User("Alice");
user.sayHi(); // this → user → "Alice"If you call the method on its own (losing the context), this will be undefined (in strict mode):
const say = user.sayHi;
say(); // TypeError: Cannot read properties of undefinedClasses always run in strict mode, even if you never explicitly enabled it.
3. this in the Constructor
The constructor is called automatically with new, and this points to the new instance:
class Product {
constructor(name, price) {
this.name = name;
this.price = price;
}
}
const p = new Product("Shirt", 3000);
console.log(p.name); // ShirtIf it's not called via new → you get an error:
Product("Shirt", 3000); // TypeError: Class constructor cannot be invoked without 'new'4. this With Inheritance (extends)
If a class inherits from another, you must call super() before accessing this, otherwise you get an error.
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name); // creates this via the parent's constructor
this.breed = breed; // this can now be used
}
}If super() is not called:
class Dog extends Animal {
constructor(name) {
this.name = name; // ReferenceError: Must call super constructor before accessing 'this'
}
}5. Losing the this Context
When a method is passed as a callback, this is lost:
class User {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hi, ${this.name}`);
}
}
const u = new User("Alice");
setTimeout(u.greet, 1000); // undefined, because the context is lostSolution 1: Bind this Manually
setTimeout(u.greet.bind(u), 1000); // Hi, AliceSolution 2: Use an Arrow Function
class User {
name = "Alice";
greet = () => {
console.log(`Hi, ${this.name}`);
};
}
const u = new User();
setTimeout(u.greet, 1000); // Hi, AliceArrow functions preserve
thisfrom the surrounding context, which is why they are often used for event handlers inside a class.
6. In Static Methods, this Refers to the Class Itself
class MathUtils {
static describe() {
console.log(this); // class MathUtils { ... }
}
}
MathUtils.describe(); // this → MathUtilsIf you extend the class:
class AdvancedMath extends MathUtils {}
AdvancedMath.describe(); // this → AdvancedMathIn other words, this in a static method refers not to an instance, but to the class itself.
7. In Private and Arrow Methods
class Counter {
#count = 0;
increment = () => {
this.#count++;
console.log(this.#count);
};
}
const c = new Counter();
setTimeout(c.increment, 1000); // this is preserved, works correctlyHere
thisis not lost, because an arrow function does not create its ownthis.
Summary
| Context | Value of this |
|---|---|
| In the constructor | New class instance |
| In a regular method | The object the method was called on |
When context is lost (setTimeout(obj.method)) | undefined |
| In an arrow method | Inherited from the surrounding context (not lost) |
| In a static method | The class itself (not an instance) |
Before calling super() in a subclass | Error: this is not accessible |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.