เพื่อเรียกเมธอดพาเรนต์เมื่อทั้งพาเรนต์และลูกมีชื่อเมธอดและลายเซ็นเหมือนกัน
คุณสามารถใช้ไวยากรณ์ด้านล่าง −
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