- Introduction to Classes in JavaScript in Hindi
- Class Declarations in Hindi

Introduction to Classes in JavaScript
जावास्क्रिप्ट में, कक्षाएं विशेष प्रकार के functions हैं। हम class को फ़ंक्शन declarations और फ़ंक्शन expression की तरह परिभाषित कर सकते हैं।
जावास्क्रिप्ट class में एक body के विभिन्न class members होते हैं जिनमें methods या constructor शामिल हैं। Class को strict mode में निष्पादित किया जाता है। तो, silent error या गलती वाला कोड एक error दिखाता है।
Class syntax में दो घटक होते हैं:
- क्लास declarations
- Class expressions
Class declarations
एक वर्ग घोषणा का उपयोग करके एक वर्ग को परिभाषित किया जा सकता है। एक वर्ग कीवर्ड का उपयोग किसी विशेष नाम के साथ एक वर्ग घोषित करने के लिए किया जाता है। जावास्क्रिप्ट naming conventions के अनुसार, वर्ग का नाम हमेशा एक बड़े अक्षर से शुरू होता है।
Class declarations उदाहरण
आइए कक्षा को घोषित करने का एक सरल उदाहरण देखें।
<script>
//Declaring class
class Employee
{
//Initializing an object
constructor(id,name)
{
this.id=id;
this.name=name;
}
//Declaring method
detail()
{
document.writeln(this.id+" "+this.name+"<br>")
}
}
//passing object to a variable
var e1=new Employee(101,"Martin Roy");
var e2=new Employee(102,"Duke William");
e1.detail(); //calling method
e2.detail();
</script>
आउटपुट:
101 Martin Roy
102 Duke William
Class declarations उदाहरण: Hoisting
फ़ंक्शन डिक्लेरेशन के विपरीत, क्लास डिक्लेरेशन जावास्क्रिप्ट hoisting का एक हिस्सा नहीं है। इसलिए, इसे लागू करने से पहले कक्षा को घोषित करना आवश्यक है।
एक उदाहरण देखते हैं।
<script>
//Here, we are invoking the class before declaring it.
var e1=new Employee(101,"Martin Roy");
var e2=new Employee(102,"Duke William");
e1.detail(); //calling method
e2.detail();
//Declaring class
class Employee
{
//Initializing an object
constructor(id,name)
{
this.id=id;
this.name=name;
}
detail()
{
document.writeln(this.id+" "+this.name+"<br>")
}
}
</script>
आउटपुट:
वर्ग घोषणाएँ उदाहरण: redeclaring class
एक कक्षा को केवल एक बार घोषित किया जा सकता है। यदि हम कक्षा को एक से अधिक बार घोषित करने का प्रयास करते हैं, तो यह एक error दिखाता है।
एक उदाहरण देखते हैं।
<script>
//Declaring class
class Employee
{
//Initializing an object
constructor(id,name)
{
this.id=id;
this.name=name;
}
detail()
{
document.writeln(this.id+" "+this.name+"<br>")
}
}
//passing object to a variable
var e1=new Employee(101,"Martin Roy");
var e2=new Employee(102,"Duke William");
e1.detail(); //calling method
e2.detail();
//Re-declaring class
class Employee
{
}
</script>
आउटपुट:
Classic expressions
एक वर्ग को परिभाषित करने का दूसरा तरीका एक class expression का उपयोग करके है। यहां, class के नाम को निर्दिष्ट करना अनिवार्य नहीं है। तो, class expression को नाम दिया जा भी सकता है या नहीं भी। वर्ग अभिव्यक्ति हमें class name fetch करने की अनुमति देती है। हालांकि, यह वर्ग घोषणा के साथ संभव नहीं होगा।
Unnamed: class expression
किसी भी नाम को निर्दिष्ट किए बिना class को express किया जा सकता है।
एक उदाहरण देखते हैं।
<script>
var emp = class {
constructor(id, name) {
this.id = id;
this.name = name;
}
};
document.writeln(emp.name);
</script>
आउटपुट:
emp
क्लास एक्सप्रेशन उदाहरण: redeclaring class
वर्ग घोषणा के विपरीत, वर्ग अभिव्यक्ति हमें उसी वर्ग को फिर से घोषित करने की अनुमति देती है। इसलिए, यदि हम कक्षा को एक से अधिक बार घोषित करने का प्रयास करते हैं, तो यह एक त्रुटि फेंकता है।
<script>
//Declaring class
var emp=class
{
//Initializing an object
constructor(id,name)
{
this.id=id;
this.name=name;
}
//Declaring method
detail()
{
document.writeln(this.id+" "+this.name+"<br>")
}
}
//passing object to a variable
var e1=new emp(101,"Martin Roy");
var e2=new emp(102,"Duke William");
e1.detail(); //calling method
e2.detail();
//Re-declaring class
var emp=class
{
//Initializing an object
constructor(id,name)
{
this.id=id;
this.name=name;
}
detail()
{
document.writeln(this.id+" "+this.name+"<br>")
}
}
//passing object to a variable
var e1=new emp(103,"James Bella");
var e2=new emp(104,"Nick Johnson");
e1.detail(); //calling method
e2.detail();
</script>
आउटपुट:
101 Martin Roy
102 Duke William
103 James Bella
104 Nick Johnson
Named class expression example
हम class को विशेष नाम के साथ व्यक्त कर सकते हैं। यहाँ, class name का दायरा class body तक है। Ni
<script>
var emp = class Employee {
constructor(id, name) {
this.id = id;
this.name = name;
}
};
document.writeln(emp.name);
/*document.writeln(Employee.name);
Error occurs on console:
"ReferenceError: Employee is not defined
*/
</script>
आउटपुट:
Employee