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

ES6 क्लासेज

ES6 क्लासेज JavaScript में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (OOP) को सरल और अधिक संरचित बनाने के लिए एक महत्वपूर्ण फीचर हैं। ES6 से पहले, डेवलपर्स को ऑब्जेक्ट बनाने के लिए constructor functions और prototype chains का उपयोग करना पड़ता था, जो शुरुआती लोगों के लिए अक्सर जटिल और भ्रमित करने वाला था। क्लासेज ने इसे आसान और पठनीय बना दिया।
क्लासेज का उपयोग विभिन्न वास्तविक जीवन के प्रोजेक्ट्स में किया जा सकता है। उदाहरण के लिए, एक portfolio वेबसाइट में आप एक Project क्लास बना सकते हैं जिसमें प्रोजेक्ट का नाम, विवरण और लिंक शामिल हो। ब्लॉग में Article क्लास लेख के शीर्षक, लेखक और कंटेंट को संभाल सकती है। E-commerce प्लेटफ़ॉर्म में Product और Order क्लास का उपयोग डेटा को व्यवस्थित करने के लिए किया जा सकता है। न्यूज़ साइट पर NewsItem क्लास और सोशल प्लेटफ़ॉर्म पर User या Post क्लास बहुत उपयोगी हो सकते हैं।
ES6 क्लासेज को आप एक घर बनाने के रूपक के रूप में सोच सकते हैं: क्लास एक ब्लूप्रिंट है और हर ऑब्जेक्ट एक रूम है जिसे आप अपनी ज़रूरत के अनुसार सजाते हैं। जैसे आप कमरे की सजावट करते हैं या पुस्तकालय में किताबों को व्यवस्थित करते हैं, क्लासेज को सही तरीके से डिज़ाइन करना कोड को व्यवस्थित और maintainable बनाता है। इस ट्यूटोरियल में आप सीखेंगे क्लास कैसे बनाते हैं, constructors और methods का उपयोग कैसे करते हैं, inheritance कैसे लागू करते हैं और इन्हें वास्तविक प्रोजेक्ट्स में कैसे इस्तेमाल करते हैं।

मूल उदाहरण

javascript
JAVASCRIPT Code
// Define a simple ES6 class
class User {
constructor(name, email) { // constructor initializes instance properties
this.name = name;
this.email = email;
}
greet() { // method shared across all instances
return `Hello, ${this.name}!`;
}
}

const user1 = new User("Rohit", "[[email protected]](mailto:[email protected])");
console.log(user1.greet()); // "Hello, Rohit!"

इस उदाहरण में हमने User नामक एक क्लास बनाई है। class keyword का उपयोग करके हम ऑब्जेक्ट-ओरिएंटेड सिंटैक्स को सरलता से लागू कर सकते हैं। constructor एक विशेष method है जो new keyword से object बनाने पर automatic call होता है। इसमें हम this.name और this.email properties को इनिशियलाइज़ कर रहे हैं। this उस ऑब्जेक्ट को संदर्भित करता है जो instance बन रहा है।
greet() method क्लास में defined है और सभी instances के लिए उपलब्ध है। इस तरह methods prototype पर stored होती हैं जिससे memory का efficient उपयोग होता है।
const user1 = new User("Rohit", "[email protected]") से एक नया instance बनता है। constructor properties को सेट करता है और user1.greet() call करने पर personalized greeting "Hello, Rohit!" return होती है।
व्यावहारिक उपयोग में, ऐसी क्लास ब्लॉग या सोशल प्लेटफ़ॉर्म पर user को represent कर सकती है। एक आम beginner का confusion class और object को लेकर होता है। याद रखें, class blueprint है और object वास्तविक room है।

व्यावहारिक उदाहरण

javascript
JAVASCRIPT Code
// Class representing a product in an e-commerce site
class Product {
constructor(name, price) {
this.name = name;
this.price = price;
}
display() { // method to show product details
return `${this.name} costs ₹${this.price}`;
}
}

const product1 = new Product("Laptop", 70000);
const product2 = new Product("Headphones", 2500);

console.log(product1.display()); // "Laptop costs ₹70000"
console.log(product2.display()); // "Headphones costs ₹2500"

Common mistakes में शामिल हैं: new keyword भूल जाना, constructor में methods define करना जिससे memory leak हो सकता है, callbacks में this का गलत उपयोग और business logic को UI logic में mix करना। Debugging के लिए console.log(this) या instanceof का उपयोग करें और classes को modular रखें।

📊 त्वरित संदर्भ

Property/Method Description Example
constructor Initializes a class instance class A { constructor(x){this.x=x;} }
this Refers to current instance this.name = "Rohit";
method() Function attached to prototype greet(){ return "Hello"; }
extends Class inheritance class B extends A {}
super Calls parent constructor/method super(name);
static Class-level method static info(){ return "Details"; }

सारांश और अगले कदम:
इस ट्यूटोरियल में आपने ES6 क्लासेज की मूल बातें सीखीं: constructors, methods, instantiation और inheritance। क्लासेज code को organize और reusable बनाती हैं, जैसे एक architect घर का blueprint बनाता है।
Portfolio, Blog, E-commerce या Social Platforms में classes users, products, articles या posts को efficiently model करती हैं। ये concepts सीधे DOM manipulation और backend communication से जुड़ते हैं।
अगले चरण में, static methods, advanced inheritance और design patterns (Singleton, Factory) का अध्ययन करें। अभ्यास के लिए एक mini-blog या shopping module बनाएं। modular और clean code long-term maintainability और scalability सुनिश्चित करता है।

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

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

अपना ज्ञान परखें

व्यावहारिक प्रश्नों के साथ इस विषय की अपनी समझ का परीक्षण करें।

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

📝 निर्देश

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