JavaScriptFundamental (ES6), part 1
Why arrow functions have no own this in JavaScript
8 An arrow has no this of its own
fairInside a method, map over an array twice: once with an arrow callback and once with a plain function callback. Report what this is in each, then fix the plain one two ways.
Stuck? Three hints, smallest first
- 1. An arrow does not create a
this. It reads the one from the scope where it was written. - 2. A plain callback is called with no receiver, so under strict mode its
thisisundefined. - 3.
maptakes a second argument that becomesthisfor the callback.binddoes the same job.
const shop = {
name: 'Balogun Market',
stalls: ['A1', 'A2'],
withArrow() {
return this.stalls.map((s) => this.name + ' ' + s);
},
withPlainCallback() {
return this.stalls.map(function () {
return this === undefined ? 'lost this' : 'kept this';
});
},
withThisArg() {
return this.stalls.map(function (s) { return this.name + ' ' + s; }, this);
},
withBind() {
return this.stalls.map(function (s) { return this.name + ' ' + s; }.bind(this));
},
};
function outerThis() {
const arrow = () => this.tag;
return arrow.call({ tag: 'ignored' }); // call cannot touch an arrow
}
const plain = function () { return this.tag; };
const stall = {
code: 'A1',
viaMethod() { return this.code; },
viaArrow: () => (this && this.code) ?? 'not A1',
};
console.log('arrow callback :', shop.withArrow());
console.log('plain callback :', shop.withPlainCallback());
console.log('map(fn, this) :', shop.withThisArg());
console.log('fn.bind(this) :', shop.withBind());
console.log('arrow ignores call :', outerThis.call({ tag: 'outer' }));
console.log('arrow as a method :', stall.viaArrow(), ' method shorthand:', stall.viaMethod());
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('an arrow callback keeps the surrounding this', shop.withArrow(), ['Balogun Market A1', 'Balogun Market A2']);
test('a plain callback loses it, and under strict mode that means undefined', shop.withPlainCallback(), ['lost this', 'lost this']);
test("map's second argument puts it back", shop.withThisArg(), ['Balogun Market A1', 'Balogun Market A2']);
test('so does bind', shop.withBind(), ['Balogun Market A1', 'Balogun Market A2']);
test("call cannot change an arrow's this", outerThis.call({ tag: 'outer' }), 'outer');
test('but it does change a plain function', plain.call({ tag: 'given' }), 'given');
test('an arrow written as a method never sees the object', stall.viaArrow(), 'not A1');
test('while a method shorthand does', stall.viaMethod(), 'A1');
An arrow closes over this the same way it closes over any other variable, which is why it fixed the const self = this era. The flip side is the last two assertions: an arrow as a property of an object literal is written outside any method, so its this is whatever the surrounding scope had — never the object.
So the rule is short. Callback inside a method: arrow. Method on an object or a class that needs this: method shorthand. Never an arrow for the method itself.