ES6 Classes in JavaScript
ES6 Classes
Syntactic sugar over prototype inheritance.
Class Example
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hello, I'm ${this.name}`;
}
static species = "Human";
}
class Student extends Person {
constructor(name, age, grade) {
super(name, age);
this.grade = grade;
}
}
let student = new Student("John", 20, "A");
console.log(student.greet());Key Points
constructor()initializes object.extends, super()for inheritance.staticmethods belong to class.- Getter/setter:
get name() {}.