Definition:
It provides a model to create an object instead of declaring the same variable again and again to create multiple objects
consider a class with 50 number of students each student has same properties like name, roll no, age, etc. for each student we have to create an object with same details so instead of declaring each object again and again. we can declare a constructor function with required properties and create object using that constructor
to declare a constructor function the variable name should start with uppercare
function Student(name, rollNo, age){
this.name=name;
this.rollNo=rollNo;
this.age= age;
}
const student1= new Student("Akash", 214222002, 15);
const student2= new Student("Mohan", 214222005, 16);
const student3= new Student("Deva", 214222007, 15);
by this we can create n number of student object using this constructor function
Top comments (0)