पैरेंट मेथड को कॉल करने के लिए जब पैरेंट और चाइल्ड दोनों का मेथड नाम और सिग्नेचर समान हो।
आप नीचे दिए गए सिंटैक्स का उपयोग कर सकते हैं -
console.log(yourParentClassName.prototype.yourMethodName.call(yourChildObjectName));
उदाहरण
class Super {
constructor(value) {
this.value = value;
}
display() {
return `The Parent class value is= ${this.value}`;
}
}
class Child extends Super {
constructor(value1, value2) {
super(value1);
this.value2 = value2;
}
display() {
return `${super.display()}, The Child Class value2
is=${this.value2}`;
}
}
var childObject = new Child(10, 20);
console.log("Calling the parent method display()=")
console.log(Super.prototype.display.call(childObject));
console.log("Calling the child method display()=");
console.log(childObject.display()); उपरोक्त प्रोग्राम को चलाने के लिए, आपको निम्न कमांड का उपयोग करने की आवश्यकता है -
node fileName.js.
यहाँ, मेरी फ़ाइल का नाम है demo192.js.
आउटपुट
यह निम्नलिखित आउटपुट देगा -
PS C:\Users\Amit\javascript-code> node demo192.js Calling the parent method display()= The Parent class value is= 10 Calling the child method display()= The Parent class value is= 10, The Child Class value2 is=20