Sub topic 9
When Rules Collide: Binding Precedence
The earlier chapters covered each binding rule by itself: default binding, implicit binding through method calls, explicit binding with call and apply, hard binding with bind, and new binding. Real code often triggers more than one rule in the same call. A bound function can be stored on an object and called as a method. A method can be invoked with call. A bound function can be used with new. This chapter explains which rule wins in each of these cases and gives you a fixed procedure for working out this at any call site.
This chapter assumes you know how functions, methods, and closures work, and that you have read the previous chapters on the individual rules. The examples run in Node.js or a browser console. Unless a section says otherwise, each one starts with 'use strict' so that default binding gives undefined rather than globalThis.
The Ranking
The four rules rank like this, from strongest to weakest:
| Rank | Rule | How you recognize it | Value of `this` |
|---|---|---|---|
| 1 | `new` binding | `new fn(...)` | A freshly created object |
| 2 | Explicit and hard binding | `fn.call(x)`, `fn.apply(x)`, or a function returned by `fn.bind(x)` | `x` |
| 3 | Implicit binding | `obj.fn(...)` or `obj['fn'](...)` | `obj` |
| 4 | Default binding | `fn(...)` with nothing else | `undefined` in strict code, `globalThis` in sloppy code |
Explicit and hard binding share a rank, but they are not equal. Once bind has fixed this, a later call, apply, or second bind cannot change it. Hard binding therefore beats a plain call on the same function.
Arrow functions sit outside this ranking. They have no this of their own, so no rule applies to them. They read this from the enclosing scope, the way they read any other variable.
Explicit Beats Implicit
When a method is called through call or apply, the object before the dot has no effect. Only the first argument to call counts.
'use strict';
function whoAmI() {
return this?.name;
}
const a = { name: 'a', whoAmI };
const b = { name: 'b' };
console.log(a.whoAmI()); // 'a' implicit binding
console.log(a.whoAmI.call(b)); // 'b' explicit beats implicit
console.log(a.whoAmI.apply(b)); // 'b' same with applyThe reason is mechanical. a.whoAmI is only a property lookup that produces the function. .call(b) then invokes that function with b as this. The object a was used to find the function and plays no part in the call.
Hard Binding Beats Implicit and Explicit
A function returned by bind is a separate exotic object, called a bound function. It stores the target function, the bound this, and any bound leading arguments. When you call it, it ignores whatever this the call site supplies and calls the target with the stored value.
'use strict';
function whoAmI() {
return this?.name;
}
const a = { name: 'a' };
const b = { name: 'b' };
const boundToB = whoAmI.bind(b);
const c = { name: 'c', whoAmI: boundToB };
console.log(c.whoAmI()); // 'b' bind beats implicit
console.log(boundToB.call(a)); // 'b' bind beats call
console.log(boundToB.bind(a)()); // 'b' the first bind winsThe last line catches people out. Calling bind on a bound function does create a new bound function, but the new one just forwards to the old one, and the old one still uses b. A second bind can add more leading arguments. It cannot change this.
new Beats Hard Binding
new is the strongest rule. When a bound function is called with new, the bound this is thrown away and a fresh object is created instead. The bound arguments are kept, though, and that makes bind work for partial application of constructors.
'use strict';
function Point(x, y) {
this.x = x;
this.y = y;
}
const target = { x: 'untouched' };
const PointAtX10 = Point.bind(target, 10);
const p = new PointAtX10(20);
console.log(p); // Point { x: 10, y: 20 }
console.log(target.x); // 'untouched'
console.log(p instanceof Point); // true
console.log(p instanceof PointAtX10); // true
console.log(PointAtX10.prototype); // undefinedSome details here matter in practice:
- The new object's prototype is
Point.prototype. A bound function has noprototypeproperty of its own, sonewreads it from the target function. instanceofagainst a bound function checks against the target, which is why bothinstanceoftests returntrue.targetis never modified. If the boundthiswon overnew,target.xwould have become10.
Why not new with call?
You cannot apply new and call to the same invocation. Writing new whoAmI.call(b) makes JavaScript try to construct whoAmI.call, which is Function.prototype.call, and that is not a constructor:
'use strict';
function whoAmI() {
return this?.name;
}
try {
new whoAmI.call({ name: 'b' });
} catch (err) {
console.log(err instanceof TypeError); // true
}So new and explicit binding only meet through bind, and in that case new wins.
When the constructor returns an object
The new rule has an exception inside it. If a constructor explicitly returns an object, that object becomes the result of the new expression and the freshly created this is discarded. Returning a primitive is ignored.
'use strict';
function Weird() {
this.created = true;
return { replaced: true };
}
console.log(new Weird()); // { replaced: true }This does not change what this is inside the constructor. It changes only what the caller gets back. Factories and some singleton patterns depend on it. Outside those patterns it usually indicates a bug.
Arrow Functions Ignore Every Rule
An arrow function's this is fixed when the arrow is created. No call-site rule can change it, and new is not allowed at all.
'use strict';
const owner = {
name: 'owner',
makeArrow() {
return () => this.name;
}
};
const arrow = owner.makeArrow();
const holder = { name: 'holder', arrow };
console.log(arrow()); // 'owner'
console.log(holder.arrow()); // 'owner' implicit ignored
console.log(arrow.call({ name: 'x' })); // 'owner' call ignored
console.log(arrow.bind({ name: 'x' })()); // 'owner' bind ignored
try {
new arrow();
} catch (err) {
console.log(err.message); // 'arrow is not a constructor'
}call and bind still work on arrows for their argument-passing behavior. Only the this argument is silently ignored. Because no error is raised, a bug like arrow.call(obj) can go unnoticed for a long time.
A Decision Procedure
To work out this at any call site, ask these questions in order and stop at the first "yes":
- Is the function an arrow function? Then
thisis whateverthiswas in the scope where the arrow was written. - Is it called with
new? Thenthisis a new object whose prototype is the constructor's (or the bound target's)prototype. - Is it a bound function? Then
thisis the value passed to the firstbind. - Is it called through
callorapply? Thenthisis the first argument, adjusted for sloppy mode as described below. - Is it called as a property of an object, as in
obj.fn()? Thenthisisobj. - Otherwise,
thisisundefinedin strict code andglobalThisin sloppy code.
Edge Cases That Blur the Rules
Passing null or a primitive to call
In sloppy mode, explicit binding is not entirely explicit. If you pass null or undefined, the function receives globalThis, which is default binding in practice. If you pass a primitive, it is wrapped in its object form. Strict functions receive exactly what you pass. Run this one as a classic script or a CommonJS file, not as an ES module, because modules are always strict:
function sloppyThis() {
return this;
}
function strictThis() {
'use strict';
return this;
}
console.log(sloppyThis.call(null) === globalThis); // true
console.log(typeof sloppyThis.call(5)); // 'object'
console.log(strictThis.call(null)); // null
console.log(typeof strictThis.call(5)); // 'number'A common old idiom, fn.apply(null, args), depends on this. It means "I do not care about this". With a sloppy function that touches this, it can quietly read or write globals.
Syntax that keeps or drops implicit binding
Implicit binding depends on the call expression being shaped like a property access. Some syntax keeps that shape and some does not:
'use strict';
const obj = {
name: 'obj',
get() {
return this?.name;
}
};
console.log(obj.get()); // 'obj'
console.log((obj.get)()); // 'obj' grouping parentheses keep the reference
console.log(obj?.get()); // 'obj' optional chaining keeps it
console.log((0, obj.get)()); // undefined the comma operator returns a bare function
const f = obj.get;
console.log(f()); // undefined default bindingBundlers and transpilers sometimes output (0, obj.get)() on purpose so that a function is called without a receiver. If you see that pattern in compiled code, it means default binding.
super calls keep the current this
super.method() looks like a method call on super, but this inside method is the current this, usually the instance, and not the parent prototype. Implicit binding works here as though the call were this.method(), with the lookup starting one level higher in the prototype chain.
Choosing Deliberately
Knowing the ranking helps you pick the weakest rule that does the job:
| Need | Reach for | Why |
|---|---|---|
| A one-off call with a chosen receiver | `call` or `apply` | No new function is created, and `this` is visible at the call site |
| A callback that must keep its receiver | `bind` or an arrow | Survives being passed around, and nothing downstream can change it |
| A callback whose receiver a library sets on purpose, such as a DOM listener | A regular function | Hard binding or an arrow would override the library's choice |
| Pre-filling constructor arguments | `bind` with `new` | `new` discards the bound `this` and keeps the arguments |
The stronger rules give you predictability and take away flexibility. A bound or arrow function cannot be redirected, which is good for event callbacks and bad for functions meant to be borrowed as methods.
Exercise
Given 'use strict'; function f() { return this?.id; } const g = f.bind({ id: 1 }); const o = { id: 2, g };, what do o.g(), g.call({ id: 3 }), and new g().constructor === f evaluate to, and which rule decides each?
Show answer
o.g() returns 1 because hard binding beats implicit binding. g.call({ id: 3 }) returns 1 because a bound function ignores the this passed by call. new g().constructor === f is true because new beats hard binding: a fresh object is created with f.prototype as its prototype, and f.prototype.constructor is f.
Exercise
A teammate writes const handler = () => this.save(); handler.call(form); inside a module's top level and is surprised that it throws. Why does it throw, and what is the smallest fix?
Show answer
An arrow function takes this from the scope where it was written. At the top level of an ES module, this is undefined, so this.save throws a TypeError, and call cannot override an arrow's this. The fix is to use a regular function, const handler = function () { this.save(); };, so that call(form) sets this to form, or to skip this entirely and call form.save() directly.
Exercise
In a sloppy-mode script, function inc() { this.count = (this.count || 0) + 1; } inc.apply(null); runs without error. What did it modify, and how would adding 'use strict' inside inc change the result?
Show answer
In sloppy mode, passing null to apply makes this fall back to globalThis, so the call created or incremented a global count property. With 'use strict' inside inc, this would be exactly null, and reading this.count would throw a TypeError. The bug becomes visible instead of quietly writing a global.