πŸ”΄ Hard'this' BindingπŸ’» Output Question

this in class methods

πŸ’‘

Hint

Class methods are not auto-bound. Always bind in constructor or use arrow class fields.

What does this output?

class Counter {
  count = 0;

  increment() {
    this.count++;
    console.log(this.count);
  }
}

const c = new Counter();
const inc = c.increment;
inc();

Correct Output

TypeError: Cannot set properties of undefined

Why this output?

Explanation: Extracting the method loses the class instance as this. In strict mode (classes always use strict), this is undefined. Accessing undefined.count throws a TypeError.

Key Insight: Class methods are not auto-bound. Always bind in constructor or use arrow class fields.

More 'this' Binding Output Questions

🟑 MediumMethod extracted from objectβ†’πŸŸ‘ MediumArrow function thisβ†’πŸ”΄ Hardthis in nested functionβ†’πŸ”΄ Hardbind creates new functionβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz