Constructor

JavaScript Constructor is an instance of a class or function. To create a Constructor in JS, new keyword is used.

In last two articles, we have studied JavaScript Arrays and JavaScript Objects. All objects and arrays in JS are instance of build-in array and objects. That's why we use new keyword to declare an array or object which means a new instance of Array or Object.

Define user-define object


    function Car(x,y,z){ }

Create instance of object


    var car1=new Car(){ };

Object Type

The Object Type is defined by declaring a function (both declaration or expression). We define a normal JavaScript Function with parameters. Now use this keyword in function ( which refers to current object and assign properties to this. The value of this.property is parameter of function. See example


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

The typeof Car here is a function as it is defined using function keyword.

Constructor Object using new instance

Now to create instance of object, we use new keyword. Every time we use new keyword, we are actually creating a new instance of object.

Create Instance using new


    var swift=new Car("swift","swift lxi","Petrol");
    

More Instance using new


    var baleno=new Car("baleno","alpha","Petrol");
    
    var ciaz=new Car("ciaz","alpha","Petrol");

The swift, baleno and ciaz are constructor object or instance of Car function with different properties.

Car {name: "baleno", model: "alpha", fuel: "Petrol"}


    var baleno=new Car("baleno","alpha","Petrol");
    
    console.log(car);
    

Check Properties

Car name is baleno

Car model is alpha

Car fuel is petrol


    var baleno=new Car("baleno","alpha","Petrol");
    
    console.log("Car name is " + car.name);
    console.log("Car model is " + car.model);
    console.log("Car fuel is " + car.fuel);
    

Properties of constructor objects

  1. All the car names are instance of the Car Function .
  2. The data type of car names is an object.
  3. new keyword can create instance of both functions and class in ES6.
  4. Properties of each individual object can vary depends on arguments passed in new constructor.
  5. To add property to parent function, i.e. Car, use prototype

Prototype

As we already know that JavaScript is prototype based language because Javascript supports Prototype inheritance Model. This might be difficult for Java and C++ developers to understand that how Javascript is dynamic and supports OOPS without class.

JavaScript introduced classes in ES2015 (ECMA6). But still after adding class in JS programming, JavaScript is still prototype-based language.

Add prototype property in objects


    Car.prototype.parent="Suzuki";
    Car.prototype.country="Japan";
    

Check prototype property

Japan Suzuki


    console.log(swift.parent,swift.country);

Japan Suzuki


    console.log(baleno.parent, swift.country);

As we can see, the Prototype Property assigned to parent Car function is now also property of constructor object, i.e swift and baleno.

hasownproperty

country and parent are prototype properties of Car. But each car has his own properties like, name, model and fuel. This mean all cars will have five properties, name, model, fuel, country and parent. To check own property of object, use hasownpropert. See example

true


    console.log(baleno.hasOwnproperty("name"));;

false


    console.log(baleno.hasOwnproperty("parent"));;

__proto__ property is also used to check prototype properties.

obj.__proto__.name is undefined but obj.__proto__.country is japan.

Avoid using __proto__ property as it is deprecated now.