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

this in nested function

πŸ’‘

Hint

Regular nested functions lose the outer this. Fix: use arrow function or const self = this.

What does this output?

const obj = {
  value: 42,
  getValue() {
    function inner() {
      return this?.value;
    }
    return inner();
  }
};

console.log(obj.getValue());

Correct Output

undefined

Why this output?

Explanation: inner() is called as a plain function, not as a method. So this inside inner is undefined (strict) or global. It does NOT inherit obj's this.

Key Insight: Regular nested functions lose the outer this. Fix: use arrow function or const self = this.

More 'this' Binding Output Questions

🟑 MediumMethod extracted from objectβ†’πŸŸ‘ MediumArrow function thisβ†’πŸ”΄ Hardbind creates new functionβ†’πŸŸ‘ MediumsetTimeout this lossβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz