DEV Community

Sowntharya
Sowntharya

Posted on Edited on

JavaScript Constructors: Function vs Constructor

JavaScript is a programming language that allows us to create reusable code using functions and objects.
A function is generally used to perform a task.
A constructor is used to create and initialize objects.

1. What is a Function in JavaScript?
A function is a block of reusable code designed to perform a particular task.

We can define a function once and call it whenever we need it.

function greet() {
    console.log("Hello, Welcome to JavaScript!");
}

greet();   //Hello, Welcome to JavaScript!

Enter fullscreen mode Exit fullscreen mode

2. What is a Constructor in JavaScript?
A constructor is a special way of creating and initializing objects.

Constructors are commonly used when we want to create multiple objects with similar properties and methods.

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

let student1 = new Student("John", 20);
let student2 = new Student("David", 22);

console.log(student1);  //Student { name: 'John', age: 20 }
console.log(student2);  //Student { name: 'David', age: 22 }
Enter fullscreen mode Exit fullscreen mode
  • Student is a constructor function.
  • this.name stores the student's name.
  • this.age stores the student's age.
  • new is used to create a new object.
  • student1 and student2 are two different objects created using the same constructor.

3. What is the new Keyword?
The new keyword plays an important role when working with constructor functions.

When we call a constructor using new.

  • Creates a new empty object.
  • Sets the constructor's this value to that new object.
  • Executes the constructor function.
  • Returns the newly created object.
function Employee(name, salary) {
    this.name = name;
    this.salary = salary;
}

let emp1 = new Employee("Sam", 30000);

console.log(emp1.name);     //Sam
console.log(emp1.salary);  //30000
Enter fullscreen mode Exit fullscreen mode

this keyword in Constructor (Java)
this keyword in JavaScript Constructor
In JavaScript, the this keyword inside a constructor refers to the current object being created.

It is used to assign values to the object's properties.

this() is not used to call another constructor in javascript.

4. Function vs Constructor in JavaScript

5. Constructor Using ES6 Class
JavaScript introduced the class syntax in ES6, which provides a cleaner way to create objects.

class Car {
    constructor(brand, color) {
        this.brand = brand;
        this.color = color;
    }
}

let car1 = new Car("BMW", "Black");
let car2 = new Car("Audi", "White");

console.log(car1);   //Car { brand: 'BMW', color: 'Black' }
console.log(car2);   //Car { brand: 'Audi', color: 'White' }
Enter fullscreen mode Exit fullscreen mode
  • class Car defines a class.
  • constructor() initializes the object.
  • this refers to the newly created instance.
  • new Car() creates a new object. A class constructor must be called using new. Calling it without new results in an error.

6. When Should We Use Functions and Constructors?
Use Functions when:

  • You want to perform a particular task.
  • You want to calculate something.
  • You want to display a message.
  • You want reusable logic.

Use Constructors when:

  • You want to create multiple objects.
  • Objects share similar properties.
  • You want to initialize object values.
  • You want to organize code using object-oriented programming.

Top comments (0)