Chaining bind() Calls
What you'll learn: You'll discover that calling bind() multiple times on the same function only respects the first bind() — subsequent calls cannot override it.
The Core Idea
You might think that chaining bind() calls would let you "re-bind" a function's this value, but JavaScript doesn't work that way. Once you call bind() on a function, it creates a new function with this permanently locked in. Calling bind() again on that already-bound function simply wraps it in another layer, but the inner this is already set and cannot be changed.
Think of it like sealing a letter in an envelope. You can put that envelope into another envelope, and another, but the original letter inside never changes — it's already sealed.
How It Actually Works
const person1 = { name: 'Alice' };
const person2 = { name: 'Bob' };
const person3 = { name: 'Charlie' };
function greet() {
console.log(`Hello, ${this.name}`);
}
const boundOnce = greet.bind(person1);
const boundTwice = boundOnce.bind(person2);
const boundThrice = boundTwice.bind(person3);
boundThrice(); // "Hello, Alice" — still person1!
The first bind(person1) locked this to person1. Every subsequent bind() call created a new wrapper function, but the inner this remained person1 forever.
Why This Matters
This behavior prevents accidental rebinding, but it can surprise you if you're trying to "fix" a binding by calling bind() again. Once a function is bound, that's final. If you need different bindings, call bind() on the original function, not on an already-bound version.
Key Takeaway: Only the first bind() sets this; chaining additional bind() calls wraps the function but cannot override the original binding.