Загрузка…
Загрузка…
class — синтаксический сахар над прототипным наследованием. Под капотом всё ещё prototype, но API ближе к классическому ООП.
class Person {
constructor(name) {
this.name = name;
}
greet() {
return `Hi, ${this.name}`;
}
static species() {
return "Homo sapiens";
}
}
class Employee extends Person {
constructor(name, role) {
super(name);
this.role = role;
}
}prototype, static — на самой функции-конструкторе;function — TDZ;new → TypeError;#private поля, getters/setters, extends;instanceof и цепочка прототипов те же.typeof Person; // "function"
Person.prototype.greet;«Сахар над prototypes», отличие от function-constructor, зачем super, когда предпочесть композицию вместо наследования.
