Arrow functions

In TypeScript, we can declare a function using a function expression or an arrow function. An arrow function has a shorter syntax than a function expression, and lexically binds the value of the this operator.

The this operator behaves a little differently in TypeScript and JavaScript compared to other popular programming languages. When we define a class in TypeScript, we can use the this operator to refer to the class. Let's look at an example:

class Person { 
    private _name: string; 
    constructor(name: string) { 
        this._name = name; 
    } 
    public greet() { 
        console.log(`Hi! My name is ${this._name}`); 
    } 
} 
 
let person = new Person("Remo"); 
person.greet(); // "Hi! My name is Remo" 

We have defined a Person class that contains a property ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.