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(tom); console.log(typeof Student); // function console.log(typeof Person); // undefined console.log(tom instanceof Student); // true console.log(tom instanceof Person); // ReferenceError
You will not be able to use Person
as a class (constructor function). You must use Student
, instead. The class name can be omitted to create an anonymous class in a class expression.
const Student = class { constructor(name) { this.name = name; } } const tom = new Student("Tom"); console.log(tom); console.log(typeof Student); console.log(tom instanceof Student);