Classes in javascript
Classes in JavaScript are syntactic sugar over the prototypal inheritance system. The class keyword lets you define a constructor and methods in a concise way, while under the hood it still uses prototypes. Use extends and super for inheritance.
Example
class Person {
constructor(name) {
this.name = name;
}
greet() {
return `Hi, I am ${this.name}`;
}
}
class Developer extends Person {
constructor(name, stack) {
super(name);
this.stack = stack;
}
describe() {
return `${this.name} codes in ${this.stack}`;
}
}
const dev = new Developer("Punam", "MERN");
dev.greet(); // "Hi, I am Punam"
dev.describe(); // "Punam codes in MERN"The Developer class extends Person, reusing the constructor via super and adding its own behavior; methods are shared via the prototype.