🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #53

Object Constructors

Object Constructor Functions

Sometimes we need to create many objects of the same type.

To create an object type we use an object constructor function.

It is considered good practice to name constructor functions with an upper-case first letter.

Object Type Person

function Person(first, last, age, eye) {

  this.firstName = first;

  this.lastName = last;

  this.age = age;

  this.eyeColor = eye;

}

Note: In the constructor function, this has no value. The value of this will become the new object when a new object is created. Read More: JavaScript Object this.

Now we can use new Person() to create many new Person objects:

Example

const myFather = new Person("John", "Doe", 50, "blue");

const myMother = new Person("Sally", "Rally", 48, "green");

const mySister = new Person("Anna", "Rally", 18, "green");

const mySelf = new Person("Johnny", "Rally", 22, "green");

Property Default Values

A value given to a property will be a default value for all objects created by the constructor:

Example

function Person(first, last, age, eyecolor) {

this.firstName = first;

this.lastName = last;

this.age = age;

  this.eyeColor = eyecolor;
  this.nationality = "English";

}

Adding a Property to an Object

Adding a property to a created object is easy:

Example

myFather.nationality = "English";

Note: The new property will be added to myFather. Not to any other Person Objects.


Adding a Property to a Constructor

You can NOT add a new property to an object constructor:

Example

Person.nationality = "English";

To add a new property, you must add it to the constructor function prototype:

Example

Person.prototype.nationality = "English";

Constructor Function Methods

A constructor function can also have methods:

Example

function Person(first, last, age, eyecolor) {

this.firstName = first;

this.lastName = last;

this.age = age;

this.eyeColor = eyecolor;
  this.fullName = function() {

    return this.firstName + " " + this.lastName;

  };

}

Adding a Method to an Object

Adding a method to a created object is easy:

Example

myMother.changeName = function (name) {

  this.lastName = name;

}

Note: The new method will be added to myMother. Not to any other Person Objects.


Adding a Method to a Constructor

You cannot add a new method to an object constructor function.

This code will produce a TypeError:

Example

Person.changeName = function (name) {

  this.lastName = name;

}

myMother.changeName("Doe");

Adding a new method to a constructor must be done to the constructor function prototype:

Example

Person.prototype.changeName = function (name) {

  this.lastName = name;

}

myMother.changeName("Doe");

Note: The changeName() function assigns the value of name to the person's lastName property, substituting this with myMother.


Built-in JavaScript Constructors

JavaScript has built-in constructors for all native objects:

new Object()   // A new Object object

new Array()    // A new Array object

new Map()      // A new Map object

new Set()      // A new Set object

new Date()     // A new Date object

new RegExp()   // A new RegExp object

new Function() // A new Function object

Note: The Math() object is not in the list. Math is a global object. The new keyword cannot be used on Math.


Did You Know?

Use object literals {} instead of new Object().

Use array literals [] instead of new Array().

Use pattern literals /()/ instead of new RegExp().

Use function expressions () {} instead of new Function().

Example

"";           // primitive string

0;            // primitive number

false;        // primitive boolean

{};           // object object

[];           // array object

/()/          // regexp object

function(){}; // function

Note: See Also: What are JavaScript Objects? What are Object Properties? What are Object Methods? What is this in Objects? How to Display Objects


Note: Advanced Chapters: JavaScript Object Definitions JavaScript Object Advanced this JavaScript Object Iterations JavaScript Object Getters & Setters JavaScript Object Management JavaScript Object Protection JavaScript Object Prototypes JavaScript Object Reference

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Object Constructors – FAQs

Quick answers about learning Object Constructors in JavaScript.

This free note from CodingNow 2.0 explains Object Constructors in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including Object Constructors, is 100% free with no signup required.
With focused practice, most students grasp Object Constructors in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now