July 2017
Intermediate to advanced
300 pages
5h 43m
English
A function declaration also obtained syntactic sugar. We write it now with a shorter syntax. It's remarkable that a function defined this way (fat arrow function) automatically picks up the surrounding context:
class Robot extends Machine { //... isRebel(){ const ALLOWED_NAMES = [ "R2D2", "C3PO" ]; return ALLOWED_NAMES.find(( name ) => { return name === this.name; }); } }
When using old function syntax, the callback function passed to an array's method, find, would lose the context of the Robot instance. Arrow functions, though, do not create their own context and, therefore, outer context (this) gets in the closure.
In this particular example, as it often goes with array extras, the callback body is extremely short. ...
Read now
Unlock full access