Constructor Function
Tags: javascript, oop, newbie
Published: 2019-04-03 20:55:18

---
title: Constructor Function
published: true
description: Basic introduction to constructors
tags: javascript, oop, newbie
---
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.

<pre>
<code>
function Car(){
    this.brand = "BMW",
    this.model = "3 series",
    this.year = 2019
}
</code>
</pre>

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:
<ul>
<li>
Constructors are defined with a capitalized name to distinguish them from other functions that are not constructors.</li>
<li>
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.</li>
<li>Constructors define properties and behaviors instead of returning a value as other functions might.</li>
</ul>

###Use a Constructor to Create Objects

<code>let whiteCar = new Car()</code>
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:
<pre><code>
    whiteCar.brand; // BMW
    whiteCar.model; // 3 series
    whiteCar.year; // 2019
</code></pre>

Just like any other object, its properties can be accessed and modified:
<pre>
<code>
whiteCar.brand = 'Nissan';
whiteCar.brand; // => Nissan
</code>
</pre>


###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.
<pre><code>
function Car(brand, model, year) {
  this.brand = brand;
  this.model = model;
  this.year = year;
}
</code></pre>

Then pass in the values as arguments to define each unique car into the Car constructor:
<code>
let bmwCar = new car("BMW", "3 Series", 2019);
</code>

so now bmwCar has following properties
<blockquote>
    bmwCar.brand //"BMW" <br/>
    bmwCar.model // "3 Series"<br/>
    bmwCar.year //2019<br/>
</blockquote>

<p>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.</p>
<h3>Verify an Object's Constructor with instanceof<h3>
<p>Anytime a constructor function creates a new object, that object is said to be an instance of its constructor. <br/>
JavaScript gives a convenient way to verify this with the instanceof operator. <br/>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.</p>
<pre><code>
let Car(brand, model, year) {
  this.brand = brand;
  this.model = model;
  this.year = year;
}

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

newcar instanceof Car; // true
</code></pre>

If an object is created without using a constructor, instanceof will verify that it is not an instance of that constructor:
<pre>
<code>
let newtoyota = {
    brand : "Toyota",
    model : "camery",
    year: 2010
}

newtoyota instanceof car; // false
</code>
</pre>

###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.