A callback can silently lose the object it was meant to update. Arrow functions avoid that failure by capturing this when they are created instead of recalculating it at the call site. section 6.3 section 6.3.3
An arrow is not merely shorter function syntax. It creates a function object without [[Construct]], an own this binding, an arguments object, or a prototype property. Its [[Environment]] supplies the surrounding this value and other captured variables. section 6.1 section 6.2.3
Use arrow functions when their deliberately restricted capabilities match the job:
- Preserve the surrounding this inside callbacks such as timers or event operations. section 6.2.3 section 6.3.3
- Do not use an arrow as a constructor: new Arrow() throws because the function lacks [[Construct]]. section 6.1 section 6.2.3
- Use rest parameters when access to all arguments is required, because arrows provide no arguments object. section 6.2.3
Preserving the object in a delayed callback
const cabinet = { label: 'Main cabinet', identifyBroken: function() { setTimeout(function() { console.log(this.label); }, 100); }, identifyFixed: function() { setTimeout(() => { console.log(this.label); }, 100); } }; The ordinary callback determines this when the timer invokes it; the arrow reuses the cabinet method's this. section 6.3.3 section 6.3
Table 6.1 The four forms of functions create very real differences in behavior
|
Capability
|
Declaration
|
Expression
|
Arrow
|
Class
|
|
Has [[Construct]]
|
✓
|
✓
|
✗
|
✓
|
|
Has own this binding
|
✓
|
✓
|
✗
|
✓
|
|
Has arguments object
|
✓
|
✓
|
✗
|
✓
|
|
Has prototype property
|
✓
|
✓
|
✗
|
✓
|
|
Hoisted
|
✓
|
|
✗
|
✗
|
|
Requires new
|
✗
|
✗
|
N/A
|
✓
|
Compare declarations, expressions, arrows, and classes by constructor support, this binding, arguments access, prototype properties, hoisting, and new requirements. section 6.2








