February 2018
Intermediate to advanced
298 pages
8h 22m
English
A class expression has a similar syntax to a class declaration. However, with class expressions, you are able to omit the class name. The body and behavior remain the same both ways.
Here is a code example to demonstrate how to define a class using a class expression:
const Student = class { constructor(name) { this.name = name; } } const s1 = new Student("Eden"); console.log(s1.name); //Output "Eden"
Here, we stored a reference of the class in a variable and used it to construct the objects.
The previous code is the same as this code when written using a function:
const Student = function(name) { this.name = name; }; const s1 = new Student("Eden"); console.log(s1.name); //Output "Eden"
Read now
Unlock full access