InterviewSolution
| 1. |
What is a class expression? |
|
Answer» In ES6, one way to define a class is to use the Class expression. Class expressions, like function expressions, can be named or unnamed. If the class is named, the name is unique to the class body. Prototype-based inheritance is used in JavaScript classes. var PRODUCT = class { constructor (num1, num2) { this.num1 = num1; this.num2 = num2; } multiplication() { return this.num1 * this.num2; }}console.log(new Product(5,8).multiplication());// EXPECTED output: 40The syntax of a class expression is similar to that of a class statement (declaration). Class expressions, on the other HAND, allow you to omit the class name (“binding identifier”), which is not possible with class statements. Additionally, unlike class declarations, class expressions allow you to redefine/re-declare classes WITHOUT causing any TYPE errors. It is not required to use the constructor property. The type of classes created with this keyword will always be "function." |
|