इनहेरिटेंस क्लासों के बीच पैरेंट-चाइल्ड संबंध बनाने का एक तरीका है। चाइल्ड क्लास (जिसे कभी-कभी सबक्लास कहा जाता है) के पास पैरेंट क्लास (जिसे कभी-कभी सुपरक्लास कहा जाता है) में परिभाषित व्यवहार और डेटा तक पहुँच होती है।
class Pet {
constructor(name) {
this.name = name;
}
introduce() {
console.log(`This is my pet, ${this.name}.`);
}
}
class Dog extends Pet {}
const dog = new Dog('Otis');
dog.introduce();
// => This is my pet, Otis.
चाइल्ड क्लास की घोषणा में extends कीवर्ड प्रोटोटाइप चेन के ज़रिए पैरेंट क्लास के साथ संबंध बनाता है।
चाइल्ड क्लास का कंस्ट्रक्टर जो ऑब्जेक्ट बनाता है, उनकी प्रोटोटाइप चेन में पैरेंट क्लास का प्रोटोटाइप शामिल रहता है। इससे उन ऑब्जेक्ट को पैरेंट क्लास में परिभाषित हर मेथड और डेटा तक पहुँच मिल जाती है।
const dog = new Dog('Otis');
Dog.prototype.isPrototypeOf(dog); // => true
Pet.prototype.isPrototypeOf(dog); // => true
Pet.prototype.isPrototypeOf(Dog.prototype); // => true
Pet.prototype.hasOwnProperty('introduce'); // => true
Dog.prototype.hasOwnProperty('introduce'); // => false
dog.hasOwnProperty('introduce'); // => false
अगर चाइल्ड क्लास कोई कंस्ट्रक्टर फंक्शन नहीं बनाती, तो पैरेंट क्लास का कंस्ट्रक्टर फंक्शन इस्तेमाल होता है।
लेकिन अगर चाइल्ड क्लास अपना कंस्ट्रक्टर फंक्शन खुद बनाती है, तो पैरेंट कंस्ट्रक्टर को स्पष्ट रूप से कॉल करना ज़रूरी होता है।
चाइल्ड कंस्ट्रक्टर के स्कोप के अंदर से पैरेंट कंस्ट्रक्टर को कॉल करने के लिए super कीवर्ड इस्तेमाल किया जाता है।
class Pet {
constructor(name) {
this.name = name;
}
}
class Dog extends Pet {
constructor(name, breed) {
super(name);
this.breed = breed;
}
}
const dog = new Dog('Otis', 'Pug');
पैरेंट कंस्ट्रक्टर ही नया ऑब्जेक्ट तैयार करता है और उसे this में असाइन करता है। इसलिए चाइल्ड कंस्ट्रक्टर से this इस्तेमाल करने से पहले पैरेंट कंस्ट्रक्टर को कॉल करना ज़रूरी है।
class Dog extends Pet {
constructor(name, breed) {
// using 'this' before calling the parent constructor with 'super'
this.breed = breed;
super(name);
}
}
const dog = new Dog('Otis', 'Pug');
// => ReferenceError: Must call super constructor in derived class before accessing 'this'...
चाइल्ड क्लास पैरेंट से मिले व्यवहार के अलावा अपना खुद का व्यवहार भी बना सकती है।
इनहेरिटेंस इस्तेमाल करने की एक बड़ी वजह यही है। इससे ऐसी विशेष चाइल्ड क्लास बनती हैं जिनके अपने अलग डेटा और मेथड होते हैं, और ये सब पैरेंट क्लास के साझा मेथड और डेटा से जुड़े रहते हैं।
class Dog extends Pet {
constructor(name, breed) {
super(name);
this.breed = breed;
}
describe() {
console.log(`${this.name} is a ${this.breed}.`);
}
}
const dog = new Dog('Otis', 'Pug');
dog.introduce();
dog.describe();
// => 'This is my pet, Otis.'
// => 'Otis is a Pug.'
चाइल्ड क्लास पैरेंट में बने किसी मेथड के व्यवहार को ओवरराइड भी कर सकती है। वह उसे अपने व्यवहार से बदल सकती है या बढ़ा सकती है।
class Cat extends Pet {
// replacing parent class behavior
introduce() {
console.log(`This is my cat, ${this.name}.`);
}
}
class Dog extends Pet {
constructor(name, breed) {
super(name);
this.breed = breed;
}
describe() {
console.log(`${this.name} is a ${this.breed}.`);
}
// extending parent class behavior
introduce() {
super.introduce();
this.describe();
}
}
const cat = new Cat('Milo');
cat.introduce();
// => 'This is my cat, Milo.'
const dog = new Dog('Otis', 'Pug');
dog.introduce();
// => This is my pet, Otis.
// => Otis is a Pug.
चाइल्ड क्लास पर उसी नाम का मेथड हो, और उसके अंदर से पैरेंट क्लास का मेथड कॉल करना हो, तो इसके लिए super कीवर्ड से पैरेंट को रेफर करना ज़रूरी है।