What This Refers To

Talk it through out loud with your AI coach.

The problem

  • medium
  • Pro

A class has a method that reads this dot name. Calling it directly works, but passing the method to a button's click handler or to a timer means this no longer refers to the object. Explain how JavaScript decides what this means, and give two ways to fix the bug.

What a strong answer covers

  • How JavaScript decides what this means for an ordinary function.
  • Why handing a method to someone else to call later loses its object.
  • More than one way to keep the method tied to its instance.
  • How arrow functions treat this differently.

Common mistake: Believing this is fixed by where a method is written, rather than by how it is called.

Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.

Practise it out loud