Sub topic 9
When Rules Collide: Binding Precedence
This chapter assumes you already know the four binding rules covered earlier: default binding, implicit binding through method calls, explicit binding with call, apply, and bind, and new binding. Sometimes more than one rule seems to apply to the same call. JavaScript settles these cases with a fixed ranking.
The Ranking
When a call site matches several rules, the highest-ranked rule decides this:
| Rank | Rule | Call shape | `this` becomes |
|---|---|---|---|
| 1 | `new` binding | `new fn()` | The newly created object |
| 2 | Explicit binding | `fn.call(obj)`, `fn.apply(obj)`, `fn.bind(obj)` | `obj` |
| 3 | Implicit binding | `obj.fn()` | `obj` |
| 4 | Default binding | `fn()` | `undefined` in strict mode, `globalThis` in sloppy mode |
Arrow functions are left out of the table on purpose. They have no this of their own, so none of these rules apply to them. The next chapter covers them.
Explicit Beats Implicit
A method call through an object loses to call or apply. A bound function also ignores the object it is called on:
'use strict';
function whoAmI() {
return this.name;
}
const alice = { name: 'Alice', whoAmI };
const bob = { name: 'Bob' };
console.log(alice.whoAmI()); // 'Alice' implicit binding
console.log(alice.whoAmI.call(bob)); // 'Bob' explicit beats implicit
const boundToBob = whoAmI.bind(bob);
alice.whoAmI = boundToBob;
console.log(alice.whoAmI()); // 'Bob' bound beats implicit
console.log(boundToBob.call(alice)); // 'Bob' call cannot rebind
console.log(boundToBob.bind(alice)()); // 'Bob' the first bind winsThe last two lines catch a lot of people. A bound function stores its target this internally and ignores any this passed to it later. Calling bind again creates a new wrapper, and that wrapper's this gets dropped by the inner bound function. You can still add more preset arguments in later bind calls. You cannot change this.
new Beats bind
new is the only rule that overrides a bound this. It still keeps the bound function's preset arguments:
function Point(x, y) {
this.x = x;
this.y = y;
}
const fixed = { x: 0, y: 0 };
const PointAtX5 = Point.bind(fixed, 5);
const p = new PointAtX5(10);
console.log(p.x, p.y); // 5 10
console.log(fixed.x); // 0, the bound object was never touched
console.log(p instanceof Point); // trueThis behavior lets you use bind for partial application of constructor arguments. new throws a TypeError on arrow functions and on most built-in methods, because they are not constructors.
Edge Case: Passing null or undefined Explicitly
Explicit binding with null or undefined acts differently depending on mode:
function sloppyShow() {
return this;
}
function strictShow() {
'use strict';
return this;
}
console.log(sloppyShow.call(null) === globalThis); // true in a classic script
console.log(strictShow.call(null)); // null
console.log(typeof sloppyShow.call(42)); // 'object', boxed to Number
console.log(typeof strictShow.call(42)); // 'number'Sloppy-mode functions replace null and undefined with globalThis and wrap primitives in objects. Strict-mode functions receive the exact value you pass. In sloppy code, fn.apply(null, args) therefore falls back to default binding. In strict code it gives this the value null.
Common Mistakes
- Expecting
obj.method()to useobjwhenmethodis a bound function. The boundthiswins. - Trying to "fix" a bound function with
call. The call runs, but it ignores the newthis. - Assuming
bindstopsnewfrom creating a fresh object. It does not.
Exercise
In strict mode, given function f() { return this.tag; }, const a = { tag: 'A' }, const b = { tag: 'B', f: f.bind(a) }, what do b.f(), b.f.call({ tag: 'C' }), and new (f.bind(a))().constructor === f return?
Show answer
b.f() returns 'A' because the bound this beats implicit binding. b.f.call({ tag: 'C' }) also returns 'A' because call cannot rebind a bound function. The new expression returns true: new overrides the bound this and creates a fresh object whose prototype is f.prototype, so its constructor is f.
Exercise
Rank these from highest to lowest precedence: implicit binding, new binding, default binding, explicit binding.
Show answer
new binding, explicit binding, implicit binding, default binding.