Creating classes and objects In Hindi
जावास्क्रिप्ट प्रोग्रामिंग में क्लास और ऑब्जेक्ट के लिए (OOPS) एक ओल्ड ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग फंडामेंटल प्रोग्रामिंग कन्सेप्ट हैं। जावास्क्रिप्ट प्रोग्राम में क्लास फीचर्स नए क्लास ऑब्जेक्ट इंस्टेंस को क्रिएट करने का एक बेहतर प्रोसेस है, और जावास्क्रिप्ट में क्लास ऑब्जेक्ट क्लास का एक इंस्टेंस फीचर्स होता है. जो की क्लास डेटा और क्लास मेथड्स को क्रिएट और मैनेज करने में हेल्पफुल होते हैं।

Creating a Class in JavaScript Programming.
जावास्क्रिप्ट प्रोग्रामिंग में क्लास को बिल्-इन क्लास कीवर्ड का उपयोग करके डिक्लेअर या डिफाइन किया जाता है। जावास्क्रिप्ट में क्लास को एक ऑब्जेक्ट इंस्टैंस के रूप में उपयोग किया जाता हैं.
Features of a class.
- Class properties – जावास्क्रिप्ट प्रोग्राम में किसी ऑब्जेक्ट से रिलेटेड डेटा क्लास के प्रॉपर्टीज होते है।
- Class methods – जावास्क्रिप्ट प्रोग्राम में क्रिएटेड किसी ऑब्जेक्ट से रिलेटेड फ़ंक्शन है, जो उस क्लास के बिहैवियर को डिफाइन करते हैं।
- Class constructor – क्लास कन्स्ट्रुक्टर एक स्पेशल क्लास मेथड होते है, जिसका उपयोग मौजूदा क्लास का एक इंस्टेंस या नया क्लास ऑब्जेक्ट क्रिएट करते समय प्रोग्राम में कॉल किया जाता है।
Basic syntax of a class.
class ClassName {
constructor(class parameters) {
// let Initialize class object properties
}
// let Define class methods
methodName() {
// set class Method logic
}
}
Example of creating a class and object in JavaScript.
How to define a class in JavaScript.
तो चलिए जावास्क्रिप्ट प्रोग्राम में एक ऐसी क्लास क्रिएट करे. जिसमें उस क्लास के प्रॉपर्टीज को इनिशियलाइज़ करने के लिए एक कंस्ट्रक्टर और एम्प्लोयी का डिटेल डिस्क्रिप्शन को डिस्प्ले करने का एक बेसिक मेथड हो।
class employee {
// let create some Constructor to initialize class properties
constructor(emp_name, emp_age) {
this.emp_name = emp_name;
this.emp_age = emp_age;
}
// let create some Method to display employee class member details
display() {
console.log(`Welcome, employee name is ${this.emp_name} and age is ${this.emp_age} year old`);
}
}
Explanation of Class Data Type.
यहाँ मौजूदा क्लास में क्लास constructor(emp_name, emp_age): एक यूजर डिफाइन कंस्ट्रक्टर मेथड तब अप्लाई होती है, जब एम्प्लोयी क्लास का एक इंस्टेंस क्रिएट किया जाता है। यह क्लास प्रॉपर्टीज एम्प्लोयी नाम और एम्प्लोयी ऐज को एक पैरामीटर के रूप में इनपुट करता है, और क्लास से रिलेटेड प्रॉपर्टीज को स्टार्ट करता है।
यहाँ display(): एक क्लास मेथड है, जो एम्प्लोयी क्लास में एम्प्लोयी का नाम और एम्प्लोयी की ऐज को डिस्प्ले करता है।
Create an object from the class in JavaScript program.
तो चलिए अब हम एक, employee क्लास का एक ऑब्जेक्ट या इंस्टेंस क्रिएट करते हैं।
let employee1 = new employee(“Siddhi”, 23);
employee1.display(); // Result is – Welcome, employee name is Siddhi and age is 23 year old
Explanation of Create an object from the class.
इस क्लास प्रोग्राम में new employee(“Siddhi”, 23): employee क्लास का एक नया इंस्टेंस ऑब्जेक्ट क्रिएट करता है, जो इसे “Siddhi” एम्प्लोयी नाम और एम्प्लोयी ऐज 23 वर्ष की आयु के साथ स्टार्ट करता है।
employee1.display(): employee1 ऑब्जेक्ट पर display() मेथड को कॉल करता है।
Creating a class with multiple methods in a JavaScript program.
किसी जावास्क्रिप्ट प्रोग्राम में क्लास में उससे क्रिएट होने वाले ऑब्जेक्ट्स के लिए अलग-अलग क्लास ऑब्जेक्ट इंस्टैंस बिहैवियर को डिफाइन करने के लिए कई क्लास मेथड हो सकती हैं।
class employee {
// let create a desire Constructor to initialize class properties
constructor(name, age, id) {
this.name = name;
this.age = age;
this.id = id;
}
// let create a Method to start the car
info() {
console.log(`${this.name} ${this.age} working employee information.`);
}
// let create Method to terminate employee information
terminate() {
console.log(`${this.name} ${this.age} terminated employee detail.`);
}
}
let employeedetail = new employee(“Siddhi”, “Harry”, 2020);
employeedetail.info(); // Result – Siddhi Harry working employee information.
employeedetail.terminate(); // Result – Siddhi Harry terminated employee detail.
Explanation of multiple methods in a JavaScript.
ऊपर दिए गए क्लास प्रोग्राम में info() और terminate() क्लास मेथड को एम्प्लोयी इनफार्मेशन को डिस्प्ले और टर्मिनेट करने के लिए डिफाइन किया गया है।
employeedetail ऑब्जेक्ट एम्प्लोयी क्लास का एक एक्साम्प्ल है, और यहाँ हम डॉट नोटेशन का उपयोग करके एम्प्लोयी क्लास की मेथड को लागू करते हैं।
Getters and Setters in a Class in JavaScript.
जावास्क्रिप्ट क्लास में क्लास गेटर्स और सेटर्स स्पेशल क्लास मेथड्स हैं, इनका उपयोग किसी क्लास ऑब्जेक्ट के प्रॉपर्टीज को एक्सेस करने और उन्हें अपडेट या मॉडिफाई करने में किया जाता है।
Features of getters and setters in a class.
- Class getter – क्लास गेटर किसी क्लास के प्रॉपर्टीज वैल्यू को रिटर्न करता है।
- Class setter – क्लास सेटर यह मौजूदा किसी क्लास पॉपर्टीज़ वैल्यू सेट अपडेट या मॉडिफाई करता है।
Javascript programming examples with getter and setter.
class Rectangle {
constructor(rect_width, rect_height) {
this.rect_width = rect_width;
this.rect_height = rect_height;
}
// let create a class Getter properties for class area
get rect_area() {
return this.rect_width * this.rect_height;
}
// let define Setter for rectangle width
set width(values) {
if (values > 0) {
this._rect_width = values;
} else {
console.log(“Width must be positive.”);
}
}
// let create or modify Getter properties for class width
get width() {
return this._rect_width;
}
}
let rect = new Rectangle(14, 30);
console.log(rect.rect_area); // Result – 420
rect.width = 20; // let Set modify or update a new width of rectangle class
console.log(rect.width); // Result – 20
console.log(rect.rect_area); // Result – 420
Getters and Setters and Explanation.
- get area() – यहाँ गेटर मेथड रेक्टेंगल की चौड़ाई और ऊँचाई के आधार पर उसका पूरा क्षेत्रफल को ज्ञात करता है।
- set width(values) – जबकि सेटर मेथड इंटरनल प्रॉपर्टीज प्रॉपर्टीज _width को मॉडिफाई करने से पहले यह सुनिश्चित करने के लिए चौड़ाई को कन्फर्म करता है कि यह एक पॉजिटिव वैल्यू है।
Inheritance Features in JavaScript.
जावास्क्रिप्ट प्रोग्रामिंग में OOPS कांसेप्ट प्रोग्रामर को क्लासेस के अंदर अन्य क्लासेस से क्लास प्रॉपर्टीज और क्लास मेथड्स प्रॉपर्टीज को इनहेरिट करने की परमिशन प्रोवाइड करते है। इसे प्रोग्रामिंग में क्लास पॉपर्टीज़ इनहेरिटेंस मेथड के रूप में जाना जाता है, क्लास इनहेरिटेंस फीचर्स जावास्क्रिप्ट प्रोग्रामर को मौजूदा क्लास में सब क्लासेज या चाइल्ड क्लासेस को क्रिएट करने में सक्षम करता है, जो रुट या पैरेंट क्लास की इनहेरिटेंस कैपबिलिटी को बढ़ाते हैं।
Syntax of Inheritance Class.
class subClass extends ParentClass {
constructor(class parameters) {
super(parameters); // it used to Call the parent class constructor properties
// here it Initialize a child class properties
}
// let Define a child class methods
}
Example of inheritance program in JavaScript.
class course {
constructor(course_name) {
this.course_name = course_name;
}
coursedetail() {
console.log(`${this.course_name} Select a course.`);
}
}
class selection extends course {
constructor(course_name, select) {
super(course_name); // here it use to Call the parent class constructor properties
this.select = select;
}
medium() {
console.log(`${this.course_name} english.`);
}
displaycourse() {
console.log(`${this.course_name} a popular ${this.select}.`);
}
}
let course1 = new selection(“javascript”, “web development programming language”);
course1.coursedetail(); // Result – javascript Select a course.
course1.displaycourse(); // Result – javascript a popular web development programming language.
Explanation of Inheritance Class.
यहाँ इनहेरिट course एक पैरेंट या रुट क्लास है, जिसमें एक यूजर डिफाइन medium() क्लास मेथड है।
course1, course क्लास को एक्सटेंड करता है, medium() मेथड को इनहेरिट करता है, और एक नया मेथड displaycourse() को ऐड करता है।
super(course_name): यहाँ एक super() फ़ंक्शन का उपयोग पैरेंट क्लास (course) के कंस्ट्रक्टर को कॉल करने और course_name प्रॉपर्टी को इनिशियलाइज़ करने में होता है।
Javascript Class Static Methods.
जावास्क्रिप्ट प्रोग्रामिंग में स्टेटिक मेथड्स क्लास में ही डिफाइन होते हैं, प्रोग्रामर क्लास के इंस्टेंस पर इन्हे डिफाइन नहीं कर सकता है। जावास्क्रिप्ट में स्टेटिक मेथड्स क्लास तब उपयोगी होते हैं, जब प्रोग्रामर प्रोग्राम में कोई ऐसा ऑपरेशन अप्लाई करना चाहता हैं, जो क्रिएटेड क्लास में इंस्टेंस प्रॉपर्टीज़ पर पूर्ण डिपेंड न हो।
Example of static method in Javascript.
class numericaltesting {
static total(p, q) {
return p + q;
}
static product(p, q) {
return p * q;
}
}
console.log(numericaltesting.total(4, 8)); // Result – 12
console.log(numericaltesting.product(3, 7)); // Result – 21
Explanation of Javascript Class Static Methods.
स्टैटिक मेथड प्रोग्राम में total() और product(), numericaltesting क्लास के लिए डिक्लेअर स्टैटिक मेथड हैं।
जिससे की आप क्लास प्रोग्राम में बिना कोई इंस्टेंस बनाए सीधे क्लास पर स्टैटिक मेथड को कॉल कर सकते है।
Summary of class and inheritance concepts in JavaScript.
- JavaScript Class – जावास्क्रिप्ट प्रोग्राम में ऑब्जेक्ट और ऑब्जेक्ट वाले ऑब्जेक्ट क्लास इंस्टैंस क्रिएशन का एक फीचर्स है।
- Class Constructor – यह डिक्लेअर क्लास में एक स्पेशल मेथड है, क्लास कन्स्ट्रक्टर उपयोग क्लास के पैरामीटर एलिमेंट को क्रिएट करने में होता है।
- Class Method – यह एक क्लास फीचर है, जो किसी भी क्लास ऑब्जेक्ट के बिहैवियर को डिफाइन करता है।
- Getter and Setter Method – क्लास एलिमेंट प्रॉपर्टीज के डिफ़ॉल्ट एलिमेंट डिक्लेअर करने और जरूरत पड़ने पर उन्हें अपडेट या मॉडिफाई करने के लिए स्पेशल मेथड है।
- Inheritance Class – किसी भी रुट या सुपर स्टैटिक क्लास के आधार पर एक नई सबक्लास क्लास क्रिएट करने का प्रोसेस है, जो रुट क्लास के इंडेक्स और लिगेसी को सबक्लास में इनहेरिट करता है।
- Static Class Method – किसी क्लास में ये वे प्रोग्राम मेथड होते है, जो मौजूदा क्लास के प्रिंसिपल पर नहीं, बल्कि रुट क्लास पर ही कॉल करते है