Classes are templates for creating objects. They encapsulate data (properties)
and behavior (methods) in one structure.
2. Class Syntax
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
document.write(`Hello, my name is ${this.name}`);
}
}
let person1 = new Person("Ragini", 25);
person1.greet();
Output will appear here...
3. Inheritance
class Student extends Person {
constructor(name, age, grade) {
super(name, age);
this.grade = grade;
}
study() {
document.write(`${this.name} is studying in grade ${this.grade}`);
}
}
let student1 = new Student("Ragini", 25, "12th");
student1.greet();
student1.study();
Output will appear here...
4. Example: Class Usage
class Car {
constructor(brand, model) {
this.brand = brand;
this.model = model;
}
info() {
return `${this.brand} - ${this.model}`;
}
}
let car1 = new Car("Tesla", "Model 3");
document.write(car1.info());
Output will appear here...
Comparison Table: Classes vs Functions
Feature
Class
Function Constructor
Syntax
class Person { }
function Person() { }
Inheritance
extends + super
prototype
Readability
Modern
Old style
✅ Best Practice: Use ES6 classes for modern OOP JavaScript.
Practice Questions
Test your understanding of JavaScript Classes.
Easy
Q1: Define a basic class.
Define a class named Rectangle.
Add a constructor method that takes width and height as parameters.
Inside the constructor, assign the parameters to this.width and this.height.
Use the class keyword followed by the class name, and define a constructor method inside it to initialize the object's properties.
Assume the Rectangle class from the previous question is defined.
Create a new instance (object) of the Rectangle class with a width of 10 and a height of 5.
Store it in a variable named myRect and log its width property.
You use the new keyword followed by the class name, and pass the required arguments to its constructor.
let myRect = new Rectangle(10, 5);
document.write(myRect.width); // 10
Medium
Q3: Add a method to a class.
Inside the Rectangle class, add a method named getArea().
The method should calculate and return the area (width * height).
Create an instance of the class and log the result of getArea() to the console.
Methods are added directly inside the class body, without the function keyword. Access the object's properties using the this keyword.
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
getArea() {
return this.width * this.height;
}
}
let rect = new Rectangle(10, 5);
document.write(rect.getArea()); // 50
Medium
Q4: Implement class inheritance.
Create a new class named Square that inherits from the Rectangle class.
Add a constructor to Square that accepts a single parameter side.
Use the super() keyword to pass side as both the width and height to the parent constructor.
Use the extends keyword to create a subclass, and call super() inside the child's constructor to initialize the parent class.
class Square extends Rectangle {
constructor(side) {
super(side, side); // Passes 'side' for both width and height to Rectangle
}
}
let sq = new Square(4);
document.write(sq.getArea()); // 16
Hard
Q5: Create and call a static method.
Write a class named MathUtil.
Inside it, define a static method named add that takes two parameters (a, b) and returns their sum.
Call this method directly on the class (without creating an instance) and log the result.
Static methods are defined using the static keyword. They belong to the class itself, so they are called directly on the class name, not on an instance of the class.
class MathUtil {
static add(a, b) {
return a + b;
}
}
// Called directly on the class, without using 'new'
document.write(MathUtil.add(5, 7)); // 12