Built-in Constructor Functions in JavaScript | JavaScript & Object Oriented Programming | Part 7

In the Previous Tutorial, we learned how to create objects by using Constructor Functions. We also saw the magic of the new keyword. So far we have learned all three ways to create objects in JavaScript – Object Literals, Factory Functions and Constructor Functions.

In this tutorial we’ll learn about some built-in Constructor Functions provided by JavaScript.

Every object that we create gets some properties and methods by-default (more on this while discussing Prototypes).

Constructor property of an Object

Every Object in JavaScript gets a constructor property that returns the function used to create that object.

Lets Experiment

Update the myjs.js file with the below code –

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

var car = new Car('Volkswagen', 'Golf');

If the Live Server is running save the code, go to the browser console and run this command –

car.constructor

It should return the the Car function that created this object.

Lets do some more experiment

Go to the browser console. Create an empty Object Literal and check its constructor property –

Hey, you told the constructor property of an object returns the function that created that object. But it returned a weird function Object(). What is it?

Object() is the built-in Constructor Function provided by JavaScript that creates the Object Literal.

When we create an Object Literal using the syntax as above, internally JavaScript engine translates it into this syntax –

var myObj = new Object();

Functions are actually Objects in JavaScript.

Constructor property of a function

Like Objects, every function in JavaScript has a constructor property. As explained earlier the constructor property returns the function that creates that object. And here gets it interesting.

Who creates the function?

Lets write the following code in my myjs.js

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

Save the file, go to the browser console and run this command –

Car.constructor

It should return the following –

ƒ Function() { [native code] }

Function() is another built-in Constructor Function provided by JavaScript that creates the function.

When we declare a function like above, internally JavaScript engines translates it into this –

var Car = new Function(make, model, `
  this.make = make;
  this.model = model;
`)

Other Built-in Constructor Functions in JavaScript

JavaScript provides lots of built-in Constructor Functions. Some of them are –

  • Object()
  • Function()
  • Boolean()
  • String()
  • Number()

Summary

  • Every object has a constructor property that returns the function used to create that object.
  • JavaScript has a built-in Constructor Function Object() that creates the object literal.
  • In JavaScript, functions are also object.
  • Like objects, functions also have a constructor property.
  • JavaScript has another built-in Constructor Function Function() that creates the function.
  • There are several other build-in Constructor Functions provided by JavaScript like Boolean, String and Number.

In the Next Tutorial, we’ll learn How to implement Abstraction in JavaScript.

Leave a Reply