Class Expression
You can use class declaration in an expression!
const Student = class Person { constructor(name) { this.name = name; } } const tom = new Student("Tom"); console.log(typeof Student); console.log(tom instanceof Student); console.log(tom);
You will not be able to use Person
as a class (constructor function). You must use Student
, instead. In fact, in a class expression, the class name can be omitted to create anonymous class.
const Student = class { constructor(name) { this.name = name; } } const tom = new Student("Tom"); console.log(typeof Student); console.log(tom instanceof Student); console.log(tom);