class类声明和函数声明的不同:
函数声明可以被提升而class类声明不能被提升
class类使用前必须先被声明
const p = new Rectangle(); // ReferenceError
class Rectangle {}
class类表达式是定义class类的另一种方式,class类表达式可以被命名也可以不被命名,但是若不被命名,
// unnamed
let Rectangle = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
// output: "Rectangle"
// named
let Rectangle = class Rectangle2 {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
console.log(Rectangle.name);
// output: "Rectangle2"