DEV Community

muthukumarasamy thangavel
muthukumarasamy thangavel

Posted on

Constructor in JS

constructor:
In js constructor is an a special function or method used to create and initialize object instances.
called automatically when created object.
constructor name should be start with upper case. (Pascal notation )
this refer to current object.
Object() can be called with or without new, but sometimes with different effects. See Return value.
Inside a constructor, this -->refers to the newly created object (or) current object.
Ex:
const function name = new Object();

const emp1 = new Person("muthu",27,"indain","india","vkp@gmail.com");

function Person(name,age,nationality,country,mailid){
console.log(this.name =name);
console.log(this.age = age);
console.log(this.nationality = nationality);
console.log(this.country = country);
console.log(this.mailid = mailid);

   }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)