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

call chain with this

πŸ’‘

Hint

Methods only keep their this when called on the object. Extract them and this is lost.

What does this output?

function Person(name) {
  this.name = name;
}

Person.prototype.greet = function() {
  return 'Hi, I am ' + this.name;
};

const p = new Person('Alice');
const greet = p.greet;

console.log(p.greet());
console.log(greet());

Correct Output

Hi, I am Alice
Hi, I am undefined

Why this output?

Explanation: p.greet() calls it as a method β€” this = p. greet() calls it as plain function β€” this = global/undefined, so this.name = undefined.

Key Insight: Methods only keep their this when called on the object. Extract them and this is lost.

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