DEV Community

Nanthini Ammu
Nanthini Ammu

Posted on

Constructor Function

What is constructor function?

  • A constructor function is a function used to create multiple objects with the same structure.
  • It is like a blue print.
  • Function name starts with capital letter.
function Person(name,age)
{
    this.name = name;
    this.age = age;
        this.nationality = "Tamil";  //default value
    this.greet = function(){
    document.write(`Hi, I am ${this.name}`+"<br>");
    }
}


const varun = new Person("Varun", 6);
const Arun = new Person("Arun", 6);
varun.greet();
Arun.greet();
document.write(varun.nationality);

Enter fullscreen mode Exit fullscreen mode

Top comments (0)