Sub topic 3
When Rules Collide: Binding Precedence
This chapter assumes you already know the four binding rules covered earlier: default, implicit, explicit (call/apply/bind), and new. Here you'll see how those rules rank against each other, how arrow functions sidestep them, and how they apply in classes and callbacks.
Binding Precedence
When more than one rule could apply to a call, the higher-ranked rule decides this:
newgives a fresh object.- Explicit or hard binding (
call,apply,bind) gives the object you passed. - Implicit binding gives the object before the dot.
- Default binding gives
undefinedin strict mode andglobalThisin sloppy mode.
'use strict';
function Box(value) {
this.value = value;
}
const target = { value: 'bound' };
const BoundBox = Box.bind(target);
const obj = { make: BoundBox };
obj.make(1);
console.log(target.value); // 1, bind beats the implicit obj
const b = new BoundBox(42);
console.log(b.value); // 42, new beats bind
console.log(target.value); // still 1Arrow Functions and Lexical this
An arrow function has no this of its own. It uses the this of the scope it was defined in, and none of the four rules can change it. call, apply, and bind cannot change it, and you can't use new on an arrow at all. Arrows work well for callbacks inside methods. They are a poor choice for object methods, because an arrow method won't get the object.
const timer = {
seconds: 0,
tick() {
[1, 2, 3].forEach(() => this.seconds++); // this is timer
return this.seconds;
},
broken: () => typeof this // not timer: arrow ignores the object
};
console.log(timer.tick()); // 3this in Classes and Class Fields
Class bodies always run in strict mode. A prototype method that is pulled off the instance and called on its own gets undefined as this. An arrow function class field is created once per instance in the constructor, so it stays tied to that instance. The cost is one function per instance instead of one shared method on the prototype.
class Button {
label = 'Save';
handleClick = () => this.label;
logLabel() { return this.label; }
}
const { handleClick, logLabel } = new Button();
console.log(handleClick()); // 'Save'
try { logLabel(); } catch (e) { console.log(e instanceof TypeError); } // trueCallbacks, thisArg, and Event Handlers
Array methods such as map, forEach, and filter take an optional thisArg argument. It sets this for a regular function callback. It has no effect on an arrow callback. In the DOM, a regular function listener gets the element as this. An arrow listener does not, so read event.currentTarget instead.
const scaler = {
factor: 3,
scale(nums) {
return nums.map(function (n) { return n * this.factor; }, this);
}
};
console.log(scaler.scale([1, 2, 3])); // [3, 6, 9]Exercise
Given const f = function () { return this.id; }.bind({ id: 1 });, what does f.call({ id: 2 }) return, and why?
Show answer
It returns 1. A function made with bind has a fixed this, and call cannot override it. Only new ranks higher than bind.
Exercise
Why does arr.map(x => this.k, { k: 5 }) ignore the thisArg?
Show answer
Arrow functions take this from the scope where they are defined, so a thisArg cannot set it.