Learning TypeScript 2.x
上QQ阅读APP看书,第一时间看更新

Class expressions

We can use two different APIs to declare a class. The first one is the class declaration syntax that we used during the preceding section. The second one is an alternative syntax known as a class expression.

The following code snippet redeclares the Person class that we declared in the preceding section using the class expression syntax:

const Person = class { 
    public constructor( 
        public name: string, 
        public surname: string, 
        public email: string 
    ) {} 
    public greet() { 
        console.log("Hi!"); 
    } 
}; 

There are no differences between the creation of an instance of a class declared using the class expression syntax and one declared using the class declaration syntax:

const person = new Person( 
    "Remo", 
    "Jansen", 
    "remo.jansen@wolksoftware.com" 
);