JavaScript Classes and Inheritance Explained

Day 21/100 of JavaScript Today’s topic : Classes in JavaScript Classes in JavaScript are a cleaner syntax over prototype-based inheritance 🔹Basic class class Person { constructor(name) { this.name = name; } greet() { console.log(`Hello, ${this.name}`); } } const user = new Person("Apsar"); user.greet(); 🔹Inheritance class Student extends Person { constructor(name, course) { super(name); this.course = course; } study() { console.log(`${this.name} studies ${this.course}`); } } const s1 = new Student("Apsar", "JS"); s1.study(); 🔹Key points - "class" is syntactic sugar over prototypes. - "constructor" initializes objects. - "extends" enables inheritance. - "super()" calls parent constructor. #Day21 #JavaScript #100DaysOfCode

To view or add a comment, sign in

Explore content categories