What is a constructor in Javascript ?

MoreOnFew
Posted underJavascript

If you have been working on or learning Object Oriented JavaScript, you must have come across the word “constructor” a number of times.

A constructor is basically a function that is used to set the properties and methods of an Object.

Generally you may use “this” keyword within the Constructor function a lot and the “new” keyword to call the constructor. Let’s look at an example :

//Constructor function
/**
* The 'this' keyword in the constructor would refer to 
* the individual object instances that would be created using the 'New' Keyword
**/
function Person(name, age, gender)
{
   this.name = name;
   this.age = age;
   this.gender = gender;
}

//Creation of an instance of the Person Object

var person = new Person('John', 27, 'Male');

/* Usage */
person.name; // returns 'John'
person.age; // returns 27

How to find the constructor of an Object in JavaScript ?

Given an Object, you can find out its Constructor by using the ‘constructor’ keyword. You need to use “Object.constructor” to identify the Object’s constructor. Please note that the “Object” here would most probably be an Instance of the Object that we must have created using the “new” keyword.

E.g :

/* To find the Constructor of the instance of the Object Person
** that we created in the previous example, try the following: */

person.constructor;

/*Returns 
function Person(name, age, gender)
{
  ...
}
*/

If you notice, using the constructor object returns the complete constructor function, i.e the Person function as per our example.

In the book JavaScript The Complete Reference 3rd Edition the Author Thomas Powell explains in detail the uses of a constructor function and other object oriented JavaScript techniques. A must buy book for everybody interested in JavaScript.
Using constructors are very helpful especially if you want your code to stay organized and you want to have that extra flexibility to use features of object oriented JavaScript.

Hope you now understand what a constructor is. Let us know your thoughts on constructor.

 

TaggedConstructorJavascriptObjectOOJS


MoreOnFew.com – Read tutorials related to JavaScript, jQuery, HTML5, CSS3 and more !

Cover Image for How to check if checkbox is checked in jQuery?

How to check if checkbox is checked in jQuery?

It is very easy to check if a checkbox is checked in jQuery using the .is() or .prop() method of jQuery combined with the ‘:checked’ selector. There are other ways too like checking for the attribute checked value, however, they aren’t very reliable. The easiest and most stable way to check if checkbox is checked … Read more

MoreOnFew
Cover Image for [Solved] Attempted import error: ‘Switch’ is not exported from ‘react-router-dom’

[Solved] Attempted import error: ‘Switch’ is not exported from ‘react-router-dom’

You might be facing an “Attempted import error: ‘Switch’ is not exported from ‘react-router-dom’ ” error if you are using the react-router-dom package version 6. This error is caused due to using the older switch syntax of the react-router-dom. From version 6 onwards, the react-router-dom has replaced “Switch” with “Routes”. Let us take a look … Read more

MoreOnFew