1. You don’t need to keep typing function
    2. It lexically captures the meaning of this
    3. It lexically captures the meaning of arguments

    For a language that claims to be functional, in JavaScript you tend to be typing function quite a lot. The fat arrow makes it simple for you to create a function

    this has traditionally been a pain point in JavaScript. As a wise man once said “I hate JavaScript as it tends to lose the meaning of this all too easily”. Fat arrows fix it by capturing the meaning of this from the surrounding context. Consider this pure JavaScript class:

    1. function Person(age) {
    2. this.age = age;
    3. this.growOld = function() {
    4. this.age++;
    5. }
    6. }
    7. var person = new Person(1);
    8. setTimeout(person.growOld,1000);

    If you run this code in the browser this within the function is going to point to window because window is going to be what executes the growOld function. Fix is to use an arrow function:

    1. function Person(age) {
    2. this.age = age;
    3. this.growOld = () => {
    4. this.age++;
    5. }
    6. var person = new Person(1);
    7. setTimeout(person.growOld,1000);
    8. setTimeout(function() { console.log(person.age); },2000); // 2

    The reason why this works is the reference to this is captured by the arrow function from outside the function body. This is equivalent to the following JavaScript code (which is what you would write yourself if you didn’t have TypeScript):

    1. class Person {
    2. constructor(public age:number) {}
    3. growOld = () => {
    4. this.age++;
    5. }
    6. }
    7. var person = new Person(1);
    8. setTimeout(person.growOld,1000);
    9. setTimeout(function() { console.log(person.age); },2000); // 2

    Tip: Arrow Function Need

    Beyond the terse syntax, you only need to use the fat arrow if you are going to give the function to someone else to call. Effectively:

    1. var growOld = person.growOld;
    2. // Then later someone else calls it:
    3. growOld();

    If you are going to call it yourself, i.e.

    then this is going to be the correct calling context (in this example person).

    Tip: Arrow Function Danger

    Tip: Arrow functions with libraries that use this

    Many libraries do this e.g. jQuery iterables (one example ) will use this to pass you the object that it is currently iterating over. In this case if you want to access the library passed this as well as the surrounding context just use a temp variable like like you would in the absence of arrow functions.

    1. something.each(function() {
    2. console.log(_self); // the lexically scoped value
    3. console.log(this); // the library passed value
    4. });

    Tip: Arrow functions and inheritance

    If you have an instance method as an arrow function then it goes on this. Since there is only one this such functions cannot participate in a call to super (super only works on prototype members). You can easily get around it by creating a copy of the method before overriding it in the child.

    1. class Adder {
    2. constructor(public a: number) {}
    3. // This function is now safe to pass around
    4. add = (b: number): number => {
    5. return this.a + b;
    6. }
    7. }
    8. class ExtendedAdder extends Adder {
    9. // Create a copy of parent before creating our own
    10. private superAdd = this.add;
    11. // Now create our override
    12. add = (b: number): number => {
    13. return this.superAdd(b);
    14. }
    15. }

    Tip: Quick object return

    Sometimes you need a function that just returns a simple object literal. However, something like

    is parsed as a block containing a JavaScript Label by JavaScript runtimes (cause of the JavaScript specification).

    You can fix it by surrounding the object literal with ():

    1. // Correct ?
    2. var foo = () => ({
    3. bar: 123