लोड हो रहा है...

JavaScript क्लास

क्लास objects को constructor और methods के साथ परिभाषित करने की syntax है। new से instance बनता है और demo अंत में tea / regular / mint दिखाता है।

क्लास और constructor बनाना

Instance बनाते समय constructor चलता है और उसके fields को शुरू करता है।

Drink बनाना

javascript
class Drink {
constructor(name, size) {
this.name = name;
this.size = size;
}
}
const order = new Drink("tea", "regular");

Constructor values को नए object में रखता है। this वर्तमान instance को दर्शाता है और new Drink(...) instance बनाकर constructor चलाता है।

Instance method जोड़ना

Methods वे काम बताते हैं जो instances कर सकते हैं।

label जोड़ना

javascript
class Drink {
constructor(name, size) { this.name = name; this.size = size; }
label() { return this.name + " / " + this.size; }
}
const order = new Drink("tea", "regular");
console.log(order.label());

order.label() का परिणाम tea / regular है। Class methods prototype पर रहते हैं और instances उन्हें साझा करते हैं।

क्लास को extend करना

extends inheritance बनाता है, super(...) parent constructor चलाता है और super.label() inherited method को बुलाता है।

विशेष drink बनाना

javascript
class SpecialDrink extends Drink {
constructor(name, size, extra) { super(name, size); this.extra = extra; }
fullLabel() { return super.label() + " / " + this.extra; }
}
const special = new SpecialDrink("tea", "regular", "mint");
console.log(special.fullLabel());

SpecialDrink, label() को inherit करके extra और fullLabel() जोड़ता है। output tea / regular / mint है। Derived constructor में this से पहले super(...) चलाना जरूरी है।

Static method और private field

Static method class का होता है, instance का नहीं। #secret जैसा private field केवल class के अंदर पढ़ा जा सकता है।

Static method बुलाना

javascript
class Drink {
static kind() { return "drink"; }
#secret = "recipe";
reveal() { return this.#secret; }
}
console.log(Drink.kind());
console.log(new Drink().reveal());

Static method को Drink.kind() से बुलाएँ, new Drink().kind() से नहीं। Class के बाहर drink.#secret उपलब्ध नहीं है।

एक page में classes देखना

यह demo special instance का परिणाम document में लिखता है।

Demo चलाना

html
<p id="result"></p>
<script>
class Drink { constructor(name,size){this.name=name;this.size=size;} label(){return this.name+" / "+this.size;} }
class SpecialDrink extends Drink { constructor(name,size,extra){super(name,size);this.extra=extra;} fullLabel(){return super.label()+" / "+this.extra;} }
const order=new SpecialDrink("tea","regular","mint");
document.querySelector("#result").textContent=order.fullLabel();
</script>

Page ठीक tea / regular / mint दिखाता है। Class declaration अपने declaration से पहले उपलब्ध नहीं होती; यह temporal dead zone है, hoisting का दावा नहीं।

Classes object creation और shared behavior व्यवस्थित करती हैं। Modules files व्यवस्थित करते हैं, इसलिए वे अलग हैं; किसी एक object पर सीधे लिखा method भी अलग चीज है।

📊 Class features

Syntax काम उदाहरण
constructor(...) नए instance को शुरू करना constructor(name, size)
this वर्तमान instance this.name = name
extends और super Parent behavior reuse करना class SpecialDrink extends Drink
static Class method बनाना Drink.kind()

यह table इस lesson में इस्तेमाल हुई class syntax का सार देता है।

Practical exercise

name, size और label() वाला Drink बनाएँ। फिर extends से SpecialDrink बनाएँ, extra जोड़ें और fullLabel() से tea / regular / mint दिखाएँ। Drink.kind() भी बुलाएँ।

आम गलतियाँ

Instance method को class पर न बुलाएँ, derived constructor में super(...) न भूलें और उससे पहले this का उपयोग न करें।

मुख्य बात

Constructor fields शुरू करता है, methods prototype पर साझा होते हैं और inheritance behavior reuse करता है।

🧠 अपने ज्ञान की परीक्षा करें

शुरू करने के लिए तैयार

अपने ज्ञान की परीक्षा करें

इस इंटरैक्टिव क्विज़ के साथ अपनी चुनौती लें और देखें कि आप विषय को कितनी अच्छी तरह समझते हैं

❓
6
प्रश्न
🎯
70%
पास करने के लिए
♾️
∞
समय
🔄
∞
प्रयास

📝 निर्देश

  • हर प्रश्न को ध्यान से पढ़ें
  • हर प्रश्न के लिए सबसे अच्छा उत्तर चुनें
  • आप जितनी बार चाहें क्विज़ दोबारा दे सकते हैं
  • आपकी प्रगति शीर्ष पर दिखाई जाएगी