Skip to content

Latest commit

 

History

History
40 lines (26 loc) · 1.17 KB

README.md

File metadata and controls

40 lines (26 loc) · 1.17 KB

65. Dynamic this and binding

Learn

  • call
  • apply
  • bind
  • method calls
  • arrow lexical this

How it works

A normal function receives this from its call site. Calling object.method() supplies the object, while extracting the method usually loses that receiver. call and apply invoke with an explicit receiver; bind creates a new function with a fixed receiver. Arrow functions capture this from their surroundings and cannot be rebound.

Common mistake

Assuming this belongs to where a function was written causes detached handlers and callbacks to fail.

Guided challenge

Create an object method, detach it, then compare call, bind, and an arrow callback.

Further reading

Run the example

Open example.html, inspect example.js, then edit and refresh. For APIs that need an origin, run a local server from the JavaScripts folder.

Completion check

  • Explain the behavior and its limits in your own words.
  • Test a normal case and a failure or cleanup case.
  • Record when this feature is useful and when a simpler approach is better.