Easy'this' Binding🐛 Debug Challenge

Method detached from object loses this

Buggy Code — Can you spot the issue?

const person = {
  name: 'Alice',
  greet() { console.log('Hello, ' + this.name); },
};

const greet = person.greet;
greet();

Fixed Code

const person = {
  name: 'Alice',
  greet() { console.log('Hello, ' + this.name); },
};

const greet = person.greet.bind(person);
greet();

Bug Explained

Bug: Assigning the method to a variable detaches it from the object. When called as a plain function, this is undefined — not person.

Explanation: bind(person) permanently ties this to person regardless of how greet is later called.

Key Insight: this is determined by the call site, not the definition site. Bind when storing a method as a variable.

More 'this' Binding Debug Challenges

EasyArrow function as object method has no own thisMediumRegular callback inside method loses thisMediumbind is permanent — call cannot override it

Practice spotting bugs live →

38 debug challenges with AI hints

🐛 Try Debug Lab