Table of Contents
Introduction
हेलो दोस्तों आज हम इस post में javascript के oops concepts के बारेमे जानेगे!. javascript एक object oriented programming (OOP) language है!. जो developers को module और organized formate में code लिखने की अनुमति देती है!. तो code को एक organized formate में manage करने के लिए oops कई key concepts है तो हम one-by-one सारे concepts को हम detail और example के साथ समझेंगे!.
Class
Class javascript में object को create करने के लिए एक blueprint या template है!. यह class का structure और behavior को define करता है!. class objects के data और function को एक ही unit में encapsulate यानि की store करता है!.
Example:
<script> class Animal { constructor(name, species) { this.name = name; this.species = species; } sayHello() { console.log(`Hello, I'm a ${this.species} called ${this.name}!`); } } // Creating objects of Animal class const lion = new Animal('Leo', 'Lion'); lion.sayHello(); // Output: Hello, I'm a Lion called Leo! </script>
Object
Object को ‘new’ keyword के bad class के name से बनाया जाता है!. Object किसी class का ही एक example होता है!.
और एक object की अपनी कूद की properties और method हो सकता है!.
Example:
<script> const tiger = new Animal('Toby', 'Tiger'); tiger.sayHello(); // Output: Hello, I'm a Tiger called Toby! </script>
Inheritance
Javascript में Inheritance का मतलब एक class की सारी propeties और method को दूसरी class में inherit करने की अनुमति देता है!. जिस class से properties और method को inherit किया जाता है उसे हम superclass या parent class कह सकते है और जो class propertis और method को inherit करता है! उसे हम subclass या child class कह सकते है!. child class parent class की properties और method को override या extend कर सकते है!.
Example:
<script> class Animal { constructor(name, species) { this.name = name; this.species = species; } makeSound() { console.log('Animal makes sound...'); } } class Dog extends Animal { constructor(name, breed) { super(name, 'Dog'); this.breed = breed; } makeSound() { console.log('Dog barks...'); } fetch() { console.log('Dog fetches...'); } } const dog = new Dog('Buddy', 'Labrador'); console.log(dog.name); // Output: Buddy console.log(dog.species); // Output: Dog dog.makeSound(); // Output: Dog barks... dog.fetch(); // Output: Dog fetches... </script>
Uper के example में देख सकते है की Animal parent class है और Dog एक clild class है!. और Dog class Aminal class की method और properties को inherit कर सकते है। तो Dog class name property और makeSound() method को parent class से inherit किया है!.
Encapsulation
Encapsulation एक practice है जो objects की internal details (properties or methods) को outside world से hide करने और केवल necessary details को expose करने में मदद करता है!. यह data maintain करने और unauthorized access को रोकने में मदद करता है!.
Example:
<script> class Person { constructor(name, age) { this._name = name; // private property this._age = age; // private property } // Getter methods get name() { return this._name; } get age() { return this._age; } // Setter methods set name(newName) { this._name = newName; } set age(newAge) { if (newAge > 0) { this._age = newAge; } } sayHello() { console.log(`Hello, my name is ${this._name} and I'm ${this._age} years old.`); } } // Creating an object of Person class const person = new Person('John', 30); person.sayHello(); // Output: Hello, my name is John and I'm 30 years old. // Accessing private properties console.log(person.name); // Output: John console.log(person.age); // Output: 30 // Modifying private properties person.name = 'Jane'; // Modifying private property person.age = -5; // Trying to set invalid age console.log(person.name); // Output: Jane console.log(person.age); // Output: 30 (age remains unchanged due to validation in setter) person.sayHello(); // Output: Hello, my name is Jane and I'm 30 years old. </script>
ऊपर दिए गए example में _name and _age वह private properties है जो Person class की है और जिन्हे एक underscore prefix का इस्तेमाल करके private properties बताई गई है!. getter method name() और age() इन method private properties को access करने के लिए इस्तेमाल की गई है!. और setter method name(newName) और age(newAge) private properties को modify करने के लिए इस्तेमाल की गई है। Encapsulation properties को access और modification को control करने की अनुमति देता है!. जो data को secure करती है!.
Polymorphism
Polymorphism की different classes के objects की एक ही method name पर response करने की ability है!. javascript में Polymorphism duck typing के माध्यम से achieved की जा सकती है जहा object उनके behavior पर evaluate किया जाता है!.
Example:
<script> class Bird { fly() { console.log('Bird is flying...'); } } class Fish { swim() { console.log('Fish is swimming...'); } } // Polymorphic function function move(animal) { if (typeof animal.fly === 'function') { animal.fly(); } else if (typeof animal.swim === 'function') { animal.swim(); } else { console.log('Unknown animal!'); } } const bird = new Bird(); const fish = new Fish(); move(bird); // Output: Bird is flying... move(fish); // Output: Fish is swimming... </script>
ऊपर दिए गए example में move() function एक animal parameter लेता है और यह checks करता है की कही उसमे एक fly() और swim() वाली method है की नहीं. उसमे यह दिखता है की different classes Bird और Fish के objects को एक ही function में pass किया जा सकता है और method name move() का respond कर सकते है!.
Composition
Composition एक design pattern है जो OOP में दूसरे object से बनाये गये object को composed करने की अनुमति देता है, जो simple object को combining करके एक complex structures create करने की अनुमति देता है!.
Example:
<script> class Engine { start() { console.log('Engine started'); } } class Wheels { rotate() { console.log('Wheels rotating'); } } class Car { constructor() { this._engine = new Engine(); this._wheels = new Wheels(); } startCar() { this._engine.start(); this._wheels.rotate(); console.log('Car started'); } } const myCar = new Car(); myCar.startCar(); // Output: // Engine started // Wheels rotating // Car started </script>
ऊपर दिए गये example में Car class Engine और Wheels class से composed है Car class का startCar() method Engine और Wheels class के method इस्तेमाल करता है!. जो code को reusability और कार्य के separation को समर्थन करता है!.
Static Members
Static Members वे properties और methods है जो एक class से सबंदित है!. इन्हे सीधे class से access किया जा सकता है!. बिना class के object बनाये!.
<script> class MathUtils { static square(num) { return num * num; } static cube(num) { return num * num * num; } } console.log(MathUtils.square(5)); // Output: 25 console.log(MathUtils.cube(3)); // Output: 27 </script>
ऊपर दिए गये example में square() और cube() method static method के रूप में static keyword का इस्तेमाल करके
declared किया गया है इन method को MathUtils class के खुद पर call किया जा सकता है!.