JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript Object Constructors

5 April 2025 | Category:

A constructor is a special function used to create and initialize objects.

Think of it like a template or blueprint for creating multiple similar objects.


đŸ§± Without Constructor:

const person1 = {
  name: "Alice",
  age: 25
};

const person2 = {
  name: "Bob",
  age: 30
};

That’s fine, but what if you want to create 100 people? You’d repeat yourself a lot.


✅ With Constructor Function:

function Person(name, age) {
  this.name = name;
  this.age = age;
}

Now you can create unlimited people like this:

const p1 = new Person("Alice", 25);
const p2 = new Person("Bob", 30);

🧠 this Keyword in Constructor

Inside a constructor, this refers to the new object being created.

function Car(brand, model) {
  this.brand = brand;
  this.model = model;
}

const myCar = new Car("Toyota", "Camry");

console.log(myCar.brand);  // Toyota

🔄 The new Keyword

When you call a constructor with new, JavaScript does three things:

  1. Creates a new empty object
  2. Sets this to point to that object
  3. Returns the object automatically

🧰 Adding Methods to Constructor

You can add functions (methods) inside the constructor:

function Student(name, marks) {
  this.name = name;
  this.marks = marks;
  this.greet = function() {
    return `Hello, I'm ${this.name}`;
  };
}

const s1 = new Student("Amit", 88);
console.log(s1.greet()); // Hello, I'm Amit

🔁 Creating Multiple Objects

const user1 = new Person("Ravi", 21);
const user2 = new Person("Sita", 22);
const user3 = new Person("Dev", 23);

All these users share the same structure, but with different data.


📩 Constructor with Arrays or Objects

function Product(name, price, tags) {
  this.name = name;
  this.price = price;
  this.tags = tags;
}

const phone = new Product("iPhone", 90000, ["apple", "mobile"]);
console.log(phone.tags); // ["apple", "mobile"]

✍ Constructor with Default Values

function Book(title = "Unknown", author = "Anonymous") {
  this.title = title;
  this.author = author;
}

const b1 = new Book("1984", "George Orwell");
const b2 = new Book();

💡 Constructor vs Object Literal

FeatureObject LiteralConstructor Function
Syntax{}function MyObj() {}
ReusabilityManual copyCan create multiple
new keyword required?❌✅
Ideal forOne-time useRepeated object creation

🔐 Bonus: Constructor with Prototypes (Advanced)

To avoid duplicating methods in every object:

function Animal(name) {
  this.name = name;
}

Animal.prototype.speak = function() {
  return `${this.name} makes a sound.`;
};

const dog = new Animal("Bruno");
console.log(dog.speak()); // Bruno makes a sound.

Now all Animal objects share the same speak() method — more memory efficient!


đŸ§Ș Practice Example

function Employee(name, salary) {
  this.name = name;
  this.salary = salary;
  this.display = function () {
    return `${this.name} earns â‚č${this.salary}`;
  };
}

const emp1 = new Employee("Neha", 40000);
const emp2 = new Employee("Karan", 50000);

console.log(emp1.display());
console.log(emp2.display());

🏁 Summary

✅ Object constructors:

  • Help create multiple objects easily
  • Use this keyword for properties
  • Are called using the new keyword
  • Can include methods
  • Are memory-efficient with prototype