Arrow Functions Flashcards
What are the rules of fat arrow?
1) Take out ‘function’ keyword and replace with arrow
2) One argument doesn’t need parentheses
3) Single JavaScript expression doesn’t need return or curly braces and can be one line
4) Two arguments and no argument require ()
When would you use arrow functions? In context of ‘this’. SUPER IMPORTANT
Compact syntax is nice, but…
The problem comes to head with the keyword ‘this’
1) in jQuery, we utilized the self = this call and cached this when we passed to an iterator function, which, when utilizing this, becomes lost in the ether and no longer has the desired context of this
2) in JS, we could also use .bind(this), which holds the this context
Why do fat arrow functions help with ‘this’?
They make use of lexical this