কম্পিউটার টিউটোরিয়াল

জাভাস্ক্রিপ্টে চাইল্ড ক্লাসে একই নামের মেথড থাকলে প্যারেন্ট মেথড কল করা

ParentClass.prototype.methodName.call(childObject) সিনট্যাক্স ব্যবহার করে প্যারেন্ট ক্লাসের মেথড কল করা যায়।

উদাহরণ

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

আউটপুট

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
  1. জাভাস্ক্রিপ্ট দিয়ে পেরেন্ট এলিমেন্টের চাইল্ড এলিমেন্ট পাওয়া

  2. জাভাস্ক্রিপ্টে একই অ্যারেতে অ্যারের এলিমেন্টগুলো ডুপ্লিকেট করা

  3. জাভাস্ক্রিপ্টে H1 এর innerText চাইল্ড ছাড়া কীভাবে পাবেন?

  4. জাভাতে একই ক্লাসে একই নামের একাধিক মেথড সংজ্ঞায়িত করা যায় কিনা?