CONSTRUCTOR
The constructor in JavaScript is automatically called when the object is created. Constructor name should be start in the uppercase. Constructor is a initialization object specific value. It will reduce the repetition line of object creation.
NORMAL OBJECT CREATION
const tv1={
price:50000,
brand:"samsung"
}
const tv2={
price:60000,
brand:"Sony"
}
const tv3 ={
price:70000,
brand:"LG"
}
In this code the price and brand are repeated in all the object so instead of these we can use the constructor to reduce the code
CONSTRUCTOR
const tv1= new Tv(50000,"samsung");
const tv2= new Tv(60000,"Sony");
const tv3= new Tv(70000,"LG");
function Tv(price,brand){
this.price=price;
this.brand=brand;
}
In these code the function will call automatically once the object is created.
"this" refer to the current object
Top comments (0)