← Back to JavaScript Course | Chapter 7: OOP & Prototypes | Lesson 2 of 6

JS प्रोटोटाइपल इनहेरिटेंस

Prototypal inheritance एक object को किसी दूसरे object से features उधार लेने देता है, जैसे एक बच्चा किसी family recipe को inherit करता है। बच्चा उसमें अपने खुद के twists जोड़ सकता है।
Syntax
javascript
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

javascript
// 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

javascript
// 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

javascript
// 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

javascript
// 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

javascript
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
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Child.prototype = Parent.prototype assign करना, जो एक ही object share करता है इसलिए child में बदलाव parent को भी बदल देते हैं।
  2. prototype replace करने के बाद constructor property reset करना भूल जाना, इसलिए यह गलत function को point करता है।
  3. prototype पर array जैसा mutable data रखना, जहाँ सभी instances same array share और बदलते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.