Need Some help in Prototypical Inheritance

function Student(name, age){
    this.name = name;
    this.age = age;
}

Student.prototype.display = function(){
    return `${this.name} ${this.age} ${this.id}`;
}


function bilal(name, age,id){
    Student.call(this, name, age);
    this.id = id;
}
bilal.prototype = Object.create(Student.prototype); 
bilal.prototype.constructor = bilal;
const b = new bilal('Bila Shuja',23,'2173183');
console.log(b.display());

Why I need to initialize my class with the constructor of the class ??

bilal.prototype.constructor = bilal;

I’m certainly no expert on object prototypes, but …

For what you have, you have written a function bilal and want to use it to override the constructor you created at the top of your code. It inherits the Student functionality and adds the id. Then you make a prototype on bilal and then you overwrite the constructor.

You could have combined two of those steps with:

bilal.prototype.constructor = function bilal(name, age,id){
    Student.call(this, name, age);
    this.id = id;
};

And, if you have no reason to have a separate prototype for Student and bilal, this all could be simplified to:

function Student(name, age, id){
  this.name = name;
  this.age = age;
  this.id = id;
  this.display = function(){
    return `${this.name} ${this.age} ${this.id}`;
  }
}

const b = new Student('Bila Shuja',23,'2173183');
console.log(b.display());

But again, I’m no expert. As a professional dev, I’ve never had to deal with this kind of stuff in any depth. I think of this as one of JS’s different (failed) attempts to mimic the behavior or OOP.

Why I need to initialize my class with the constructor of the class ??

These are not “classes”, this is an object prototype, which is different than a classic “class” in OOP. In ES6, JS introduced a “class” that incorporates much of the object prototype functionality but still isn’t quite a “class” in the classic OOP meaning.

You can only go so far with OOP in JS - it’s not OOP, it’s OOP-ish. It’s also not truly FP, but FP-ish.