- What is a Constructor Function?
It is a special function used to initialize objects with specific values.
It is called automatically when a new object is created using the new keyword.
2. Naming Convention Rule:
Regular functions start with a lowercase letter (e.g., function laptop()).
Constructor functions start with an uppercase letter (e.g., function Laptop()).
Key Concept:
The this keyword refers to the current object being created.
Example Code:
function Laptop(brand, model, price, rom, ram) {
this.brand = brand;
this.model = model;
this.price = price;
this.rom = rom;
this.ram = ram;
}
const laptop1 = new Laptop("Lenovo", "l15", 45000, 512, 8);
const laptop2 = new Laptop("HP", "h15", 35000, 256, 4);
console.log(laptop1);
console.log(laptop2);

Top comments (0)