A deep dive into JavaScript's `call`, `apply`, and `bind` methods, explaining why they exist and how they work at the language mechanics level. Covers the four binding rules (default, implicit, explicit, constructor), walks through handwritten implementations of all three methods using the implicit binding trick with Symbol keys, explains the constructor edge case in `bind`, and covers the `arguments` object vs rest parameters. Includes practical use cases like fixing lost context in callbacks, method borrowing, partial application, and common pitfalls like destructuring losing `this` and arrow functions as object methods.
Table of contents
Why These APIs ExistThe Binding Rules Behind thisThe Basic Difference Between call, apply, and bindImplementing a Simple callHow call Changes thisHandling null, undefined, and Primitive ValuesWhy call Must Receive Arguments ImmediatelyImplementing applyWhy apply Was More Important Before Spread SyntaxImplementing bindbind as Partial ApplicationThe Constructor Edge Case in bindUnderstanding argumentsConverting arguments to an ArrayWhy Arrow Functions Do Not Have argumentsRest Parameters Are the Modern ReplacementPractical Differences Between call, apply, and bindReal-World Use CasesCommon PitfallsEngineering TakeawaysFinal ThoughtsSort: