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 function | Modern class |
|---|---|
| Function aur prototype alag alag likho | Sab kuch ek class block mein |
| Inheritance ke liye Object.create aur call | Sirf extends aur super |
| Private data ke liye closure tricks | Asli private #fields |
| new bhool jao to silent bug | new ke bina class call karna error deta hai |
3. Kahan use hota hai?
| Jagah | Example |
|---|---|
| Custom errors | class ValidationError extends Error |
| Data models | User, Product, Order jaise objects |
| Services | API client class jo token aur base URL sambhale |
| Web Components | class MyCard extends HTMLElement |
| Games | Player, Enemy jaise entities |
4. Kaise kaam karta hai?
| Hissa | Kaam |
|---|---|
constructor() | Object bante waqt ek dafa chalta hai |
| Public field | count = 0, har instance ki apni value |
| Private field | #balance, sirf class ke andar accessible |
| Method | Prototype par store, sab instances share karte hain |
static | Class par khud, instance par nahi |
static { } block | Class load hote waqt ek dafa setup code |
5. Code example
Basic class with private field
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
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?
| Line | Kya ho raha hai |
|---|---|
#balance = 0 | Private field, bahar se acc.#balance likhna SyntaxError hai |
static count | Class 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 Animal | Dog 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?
| Stage | Topics |
|---|---|
| Pehle | Objects, The this Keyword, Prototypes & Inheritance |
| Baad mein | Error Handling (custom error classes), Modules |
Final quick revision
| Point | Detail |
|---|---|
| Class | Objects ka blueprint, prototypes ke upar syntax |
constructor | Instance bante waqt chalta hai |
#field | Asli private data |
static | Class ka apna member, instance ka nahi |
extends / super | Inheritance aur parent ko call |
| Getter / setter | Property jaisa access, andar logic |