---
title: "Constructor functions in Javascript?"  
description: "Constructor functions in Javascript?"  
author: "Sandra Emily"  
published: 2023-10-08  
updated: 2023-10-09  
canonical: https://www.mindstick.com/forum/160067/constructor-functions-in-javascript  
category: "javascript"  
tags: ["javascript", "oops", "constructor"]  
reading_time: 2 minutes  

---

# Constructor functions in Javascript?

constructor [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) in [Javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Aryan Kumar

In JavaScript, constructor functions are a way to create objects with a specific structure and behavior. They serve as blueprints for creating multiple objects of the same type. Constructor functions are commonly used when you want to create multiple instances of objects that share the same properties and methods.

Here's how to define and use a constructor function in JavaScript:

## Defining a Constructor Function:

- A constructor function is defined using a regular JavaScript function with a capital letter at the beginning of the function name to indicate that it's intended to be used as a constructor.
- Inside the constructor function, you can define properties and methods that will be shared by all instances created from it.

```plaintext
function Person(name, age) {
  this.name = name; // Instance property
  this.age = age;   // Instance property
}

Person.prototype.sayHello = function() {
  console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
};
```

## Creating Instances:

- To create an instance of an object from the constructor function, use the **new** keyword followed by the constructor function's name.
- You can pass arguments to the constructor function to initialize instance-specific properties.

```plaintext
const person1 = new Person("Alice", 25);
const person2 = new Person("Bob", 30);
```

## Using Instances:

- You can now use these instances like regular objects, accessing their properties and calling their methods.

```plaintext
console.log(person1.name);       // "Alice"
console.log(person2.age);        // 30
person1.sayHello();              // "Hello, my name is Alice and I am 25 years old."
person2.sayHello();              // "Hello, my name is Bob and I am 30 years old."
```

## Prototype:

- You can attach methods and properties to the prototype of the constructor function to make them shared among all instances. This helps save memory as the methods are not duplicated for each object.

```plaintext
Person.prototype.isAdult = function() {
  return this.age >= 18;
};

console.log(person1.isAdult()); // true
console.log(person2.isAdult()); // true
```

Constructor functions are a fundamental concept in JavaScript's object-oriented programming. They provide a way to create and organize objects with shared properties and methods, making your code more modular and efficient when you need to create many instances of the same type of object. However, with the introduction of ES6 classes, constructor functions are often replaced with class definitions for object-oriented programming in modern JavaScript.


---

Original Source: https://www.mindstick.com/forum/160067/constructor-functions-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
