ES6 / 2015
ES20152015The largest single revision of the language. Block scoping, lexical `this`, class syntax, and a standardised Promise — the foundation of every later feature.
Arrows have no own `this`, `arguments`, or `prototype`. They close over the enclosing binding, which removed a whole genre of `var self = this` workarounds.
// In sloppy mode a plain function call rebinds `this` to the global
// object. The arrow closes over the enclosing `this` instead — the object.
const counter = {
count: 0,
startLexical() {
[1, 2].forEach(() => { this.count++; }); // arrow keeps this = counter
return this.count;
},
startClassic() {
[1, 2].forEach(function () { this.count++; }); // classic loses it
return this.count;
},
};
console.log("arrow keeps this → counter.count =", counter.startLexical());
console.log("classic loses it → counter.count still", counter.startClassic());
console.log("classic incremented a phantom global:", globalThis.count);