DEV Community

Vishang
Vishang

Posted on

Constructor Function

Constructors are functions that create new objects.
They define properties and behaviors that will belong to the new object. Think of them as a blueprint for the creation of new objects.


function Car(){
    this.brand = "BMW",
    this.model = "3 series",
    this.year = 2019
}

This constructor defines a Car object with properties name, model, and year set to BMW, 3 Series, and 2019, respectively.

Constructors follow a few conventions:

  • Constructors are defined with a capitalized name to distinguish them from other functions that are not constructors.
  • Constructors use the keyword this to set properties of the object they will create. Inside the constructor, this refers to the new object it will create.
  • Constructors define properties and behaviors instead of returning a value as other functions might.

Use a Constructor to Create Objects

let whiteCar = new Car()
Notice that the new operator is used when calling a constructor.
This tells JavaScript to create a new instance of Car called whiteCar. Without the new operator, this inside the constructor would not point to the newly created object, giving unexpected results.

Now whiteCar has all the properties defined inside the Car constructor:


    whiteCar.brand; // BMW
    whiteCar.model; // 3 series
    whiteCar.year; // 2019
Just like any other object, its properties can be accessed and modified:

whiteCar.brand = 'Nissan';
whiteCar.brand; // => Nissan

Constructors to Receive Arguments

From above example we can see that we can access and modified a value of an object by creating a new instance and apply specific values to it. it will be a lot of work if it has lots of properties to it. So to avoid this scenario we can send a value to the constructor as an arguments.


function Car(brand, model, year) {
  this.brand = brand;
  this.model = model;
  this.year = year;
}
Then pass in the values as arguments to define each unique car into the Car constructor: let bmwCar = new car("BMW", "3 Series", 2019); so now bmwCar has following properties
bmwCar.brand //"BMW"
bmwCar.model // "3 Series"
bmwCar.year //2019

The constructor is more flexible. It's now possible to define the properties for each Car at the time it is created, which is one way that JavaScript constructors are so useful. They group objects together based on shared characteristics and behavior and define a blueprint that automates their creation.

Verify an Object's Constructor with instanceof

Anytime a constructor function creates a new object, that object is said to be an instance of its constructor.
JavaScript gives a convenient way to verify this with the instanceof operator.
instanceof allows you to compare an object to a constructor, returning true or false based on whether or not that object was created with the constructor.


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

let newcar = new Car("Honda", "2011");

newcar instanceof Car; // true
If an object is created without using a constructor, instanceof will verify that it is not an instance of that constructor:

let newtoyota = {
    brand : "Toyota",
    model : "camery",
    year: 2010
}

newtoyota instanceof car; // false

Conclusion

This is very basic usage of the constructor function. It will helps you to get going and give the basic understanding of how constructor function works.

Top comments (0)