js-dojo

2 this binding

What this module drills

this is decided by how a function is called, not where it was written. There are four call forms, each with its own rule, plus one exception — arrow functions — that has no this of its own at all. Six koans, one rule each.

Why it bites

Hand a method to an event listener, a timer, or an array callback, and this is gone. Every UI toolkit and game engine has shipped this bug. Koan 05 is the one the whole dojo was built around: an arrow function stashed on a shared prototype during construction, so every instance answers for whichever one booted last.

Order

01 to 04 build the rules up (04 has you write bind by hand — do not skip it). 05 is the real-world bug; 06 shows the flip side, a method designed to be borrowed.