Skip to content
Objects In DepthChapter 29 / 47Intermediate4 min read

Classes

JavaScript classes ka modern syntax, constructor, extends, super, private

On this page

1. Classes kya hain?

Class mein aap batate hain ke object ke paas kaunsa data (fields) aur kaunse kaam (methods) honge. Phir new keyword se us blueprint ke objects (instances) bante hain.

JavaScript classes asal mein prototypes ke upar ek saaf syntax hain. Methods ab bhi prototype par hi store hote hain.

2. Kyun zaroori hai?

Constructor functions aur prototype ko manually likhna lamba aur confusing hota hai. Class syntax wahi kaam chhota aur readable bana deta hai.

Purana constructor functionModern class
Function aur prototype alag alag likhoSab kuch ek class block mein
Inheritance ke liye Object.create aur callSirf extends aur super
Private data ke liye closure tricksAsli private #fields
new bhool jao to silent bugnew ke bina class call karna error deta hai

3. Kahan use hota hai?

JagahExample
Custom errorsclass ValidationError extends Error
Data modelsUser, Product, Order jaise objects
ServicesAPI client class jo token aur base URL sambhale
Web Componentsclass MyCard extends HTMLElement
GamesPlayer, Enemy jaise entities

4. Kaise kaam karta hai?

new User() ke waqt kya hota hai
Naya khaali object banta hai
Uska prototype User.prototype set hota hai
Fields initialize hote hain
constructor chalta hai aur this naya object hai
Object return hota hai
HissaKaam
constructor()Object bante waqt ek dafa chalta hai
Public fieldcount = 0, har instance ki apni value
Private field#balance, sirf class ke andar accessible
MethodPrototype par store, sab instances share karte hain
staticClass par khud, instance par nahi
static { } blockClass load hote waqt ek dafa setup code

5. Code example

Basic class with private field

js
class BankAccount {
  #balance = 0;
  static count = 0;

  static {
    BankAccount.bankName = "ZnBank";
  }

  constructor(owner) {
    this.owner = owner;
    BankAccount.count++;
  }

  deposit(amount) {
    if (amount <= 0) throw new RangeError("Amount positive honi chahiye");
    this.#balance += amount;
    return this.#balance;
  }

  get balance() {
    return this.#balance;
  }
}

const acc = new BankAccount("Ali");
acc.deposit(500);
console.log(acc.balance, BankAccount.count, BankAccount.bankName);

Inheritance with extends aur super

js
class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    return `${this.name} awaaz nikalta hai`;
  }
}

class Dog extends Animal {
  speak() {
    return `${super.speak()}: Bhau bhau`;
  }
}

console.log(new Dog("Moti").speak());

Code kaise kaam kar raha hai?

LineKya ho raha hai
#balance = 0Private field, bahar se acc.#balance likhna SyntaxError hai
static countClass level counter, BankAccount.count se access
static { }Class define hote hi ek dafa chalta hai
get balance()Getter, acc.balance property ki tarah padhte hain
extends AnimalDog ka prototype chain Animal se jud jata hai
super.speak()Parent class ka method call

Interview tip

Child class mein agar constructor likhein to this use karne se pehle super() call karna zaroori hai, warna ReferenceError aata hai.

6. Common mistakes

Galati: Child constructor mein super() se pehle this use karna

Why? Parent ne abhi object initialize nahi kiya hota, is liye ReferenceError aata hai.

Behtar: Constructor ki pehli line mein super(args) call karein, phir this use karein.

Galati: Method ko callback mein pass karke this khona

Why? button.addEventListener('click', obj.save) mein this obj nahi rehta.

Behtar: Arrow function wrap karein ya method ko class field arrow function banayein.

Galati: Underscore wali property ko private samajhna

Why? _balance sirf ek naming convention hai, bahar se koi bhi change kar sakta hai.

Behtar: Asli privacy ke liye #balance private field use karein.

Galati: Har cheez ke liye class banana

Why? Chhote kaam ke liye class extra boilerplate aur complexity laati hai.

Behtar: Simple data ke liye plain objects aur functions kaafi hain, class tab jab state aur behaviour saath hon.

7. Pehle aur baad mein kya seekhein?

StageTopics
PehleObjects, The this Keyword, Prototypes & Inheritance
Baad meinError Handling (custom error classes), Modules
Suggested order
Objects
Prototypes
Classes
Custom Errors aur Modules

Final quick revision

PointDetail
ClassObjects ka blueprint, prototypes ke upar syntax
constructorInstance bante waqt chalta hai
#fieldAsli private data
staticClass ka apna member, instance ka nahi
extends / superInheritance aur parent ko call
Getter / setterProperty jaisa access, andar logic