JS प्रोटोटाइपल इनहेरिटेंस
In this page:
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
class Child extends Parent { }
Basic Prototypal Inheritance
Prototypal inheritance एक object को किसी दूसरे object की properties और methods दोबारा इस्तेमाल करने देता है। हर object पर method code copy करने के बजाय, prototypal inheritance prototype पर एक shared copy रखता है और हर instance को उसमें lookups delegate करने देता है।
उदाहरण: Basic Prototypal Inheritance
// Declare the constant `animal`, set to `{ speak() { console.log("Some sound"); } }`
// Declare the constant `animal`, set to `{ speak() { console.log("Some sound"); } }`
const animal = { speak() { console.log("Some sound"); } };
// Declare the constant `dog`, set to `Object.create(animal)`
// Declare the constant `dog`, set to `Object.create(animal)`
const dog = Object.create(animal);
// Call `dog.speak()`
// Call `dog.speak()`
dog.speak();
Child Properties जोड़ना
Child object की अपनी properties हो सकती हैं जबकि वह फिर भी inherited methods इस्तेमाल करता रहता है। यह आपको एक खास instance के लिए shared behavior बढ़ाने देता है, बिना उस prototype को प्रभावित किए जिस पर दूसरे objects अभी भी निर्भर करते हैं।
उदाहरण: Adding Child Properties
// Declare the constant `animal`, set to `{ speak() { console.log("Some sound"); } }`
// Declare the constant `animal`, set to `{ speak() { console.log("Some sound"); } }`
const animal = { speak() { console.log("Some sound"); } };
// Declare the constant `dog`, set to `Object.create(animal)`
// Declare the constant `dog`, set to `Object.create(animal)`
const dog = Object.create(animal);
// Assign "Rex" to `dog.name`
// Assign "Rex" to `dog.name`
dog.name = "Rex";
// Print `dog.name` to the console
// Print `dog.name` to the console
console.log(dog.name);
// Call `dog.speak()`
// Call `dog.speak()`
dog.speak();
Method Reuse
एक shared method अलग-अलग objects के साथ काम कर सकता है क्योंकि this उस object को point करता है जो method को कॉल करता है।
चूंकि prototype method एक बार define होता है, उसे update करना उस prototype से inherit करने वाले हर object के व्यवहार को बदल देता है, जो कुशल है पर जिसके साथ सावधान रहना भी ज़रूरी है।
उदाहरण: Method Reuse
// Declare the constant `animal`; its value is built below
// Declare the constant `animal`; its value is built below
const animal = {
speak() { console.log(this.name + " makes a sound"); },
};
// Declare the constant `dog`, set to `Object.create(animal)`
// Declare the constant `dog`, set to `Object.create(animal)`
const dog = Object.create(animal);
// Assign "Rex" to `dog.name`
// Assign "Rex" to `dog.name`
dog.name = "Rex";
// Call `dog.speak()`
// Call `dog.speak()`
dog.speak();
Prototype बदलना
आप चुने हुए prototype के साथ objects बना सकते हैं। Object.setPrototypeOf भी किसी prototype को बदल सकता है, पर इसे सावधानी से इस्तेमाल करना चाहिए।
Object.create(proto) इस relationship को explicitly बनाने का सीधा तरीका है, जो आपको पूरा control देता है कि ठीक कौन सा object नए object का prototype बनेगा।
उदाहरण: Changing the Prototype
// Declare the constant `proto`, set to `{ greet() { return "hi"; } }`
// Declare the constant `proto`, set to `{ greet() { return "hi"; } }`
const proto = { greet() { return "hi"; } };
// Declare the constant `obj`, set to `Object.create(proto)`
// Declare the constant `obj`, set to `Object.create(proto)`
const obj = Object.create(proto);
// Print `obj.greet()` to the console
// Print `obj.greet()` to the console
console.log(obj.greet());
Practical Inheritance Example
Prototypal inheritance तब उपयोगी है जब कई objects को हर method copy किए बिना एक जैसा व्यवहार चाहिए।
यह pattern JavaScript classes से पहले का है (जो काफी हद तक इसके ऊपर का syntax sugar हैं) और अभी भी यह बताता है कि class-based inheritance अंदर से असल में कैसे काम करती है।
उदाहरण: Practical Inheritance Example
const vehicle = { drive() { console.log("Driving"); } };
const car = Object.create(vehicle);
const truck = Object.create(vehicle);
car.drive();
truck.drive(); // both share vehicle's method
Child.prototype = Parent.prototypeassign करना, जो एक ही object share करता है इसलिए child में बदलाव parent को भी बदल देते हैं।- prototype replace करने के बाद
constructorproperty reset करना भूल जाना, इसलिए यह गलत function को point करता है। - prototype पर array जैसा mutable data रखना, जहाँ सभी instances same array share और बदलते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: