What are prototypes in JavaScript? Example Tutorial

Introduction

If you have used JavaScript, then you are definitely familiar with objects because objects are one of the commonly used data types not only in JavaScript but also in the entire programming community. An object in JavaScript looks like the following.


{

    name: “Tom”,

    age: 28,

    city: “New York

}

 


So basically, an object stores data in key-value pairs. We can create an object like we did above, and then assign it to a variable. This is one of creating objects in JavaScript. Another way is by using the constructor function. 


Every object created in JavaScript has a default property named prototype. The prototype property is often a consulting concept in JavaScript. In this article, we will discuss what the prototype property is in JavaScript and how to use it. 





Prototype

Observe the following code.


function Employee(name, age, city) {

  this.name = name;

  this.age = age;

  this.city = city;

}

 

 



“Employee” is a constructor function. Let’s see what is the value of the “Employee. prototype”.

What are prototypes in JavaScript? Example Tutorial




The prototype has several methods and properties. Observe the following image. 


What are prototypes in JavaScript



So, basically, the “Employee.prototype.constructor” is referring to “Employee” only. If we console “Employee.prototype”, then it will return only an empty object. This happens because as of now, we haven't added anything in the prototype of the “Employee” constructor. 






Adding properties

The prototype can be used to add new properties to a defined constructor function. 


Let’s create an object first. 


function Employee(name, age, city) {

  this.name = name;

  this.age = age;

  this.city = city;

}

 

const Emp1 = new Employee("Tom", 28, "New York");


The “Emp1” has three properties as of now - name, age, and city. 



Now, suppose, we want to add a new property named gender to the “Employee”. We can do this using the prototype.


Employee.prototype.gender = "male";


Now, let’s see if there is any change in “Employee”.



No, because we have added “gender” to the prototype. 



Now observe the “Emp1” object we created earlier.



The property added using the prototype is already available in the “Emp1” object.



Let’s create another object. 


function Employee(name, age, city) {

  this.name = name;

  this.age = age;

  this.city = city;

}

 

const Emp1 = new Employee("Tom", 28, "New York");

 

Employee.prototype.gender = "male";

 

const Emp2 = new Employee("Lisa", 21, "Chicago”);


The problem here is that the value of “gender” is “Emp2” is also “male”. So to change, we can simply assign a new value to it. 



This way of adding new properties to a constructor function using the prototype and then inheriting them in the objects is known as prototype inheritance.


One important point here to remember is that if we change the value of “gender”, then it will be changed for every new object but not for those which are declared before it.

function Employee(name, age, city) {

  this.name = name;

  this.age = age;

  this.city = city;

}

 

Employee.prototype.gender = "male";

 

const Emp1 = new Employee("Tom", 28, "New York");

const Emp2 = new Employee("Jack", 25, "Detroit");

 

Employee.prototype = {

  gender: "female",

};

 

const Emp3 = new Employee("Lisa", 21, "Chicago");


Let’s check the value of “gender” for every object. 



The value for “Emp3” is changed but not for “Emp1” and “Emp2” because they were declared before changing the value of “gender”.






Adding methods

Similarly, we can also use the prototype property to add new methods to a constructor function. 


Observe the following code. 


Employee.prototype.getDetails = function () {

  console.log(

    `Name of the employee: ${this.name}. \nAge: ${this.age} \nCity: ${this.city} \nGender: ${this.gender}`

  );

};


Using the prototype property, the “getDetails” method is added to the “Employee”. 


Let’s use the objects to invoke the “getDetails” method. 



It will work similarly for the other object created using “Employee”.


Now observe the following code.


Employee.prototype.getDetails = () => {

  console.log(

    `Name of the employee: ${this.name} \nAge: ${this.age} \nCity: ${this.city} \nGender: ${this.gender}`

  );

};


The “getDetails” method is an arrow function. So this means, this method does not “this” keyword, meaning, it will return undefined for name, age, city, and gender. 


Wrapping it up

So the object prototype is a simple and powerful yet confusing concept in JavaScript. As objects are an important part of modern JavaScript, it is recommended to understand the concept of prototypes. In this article, we discussed what is prototype and how it is used to add properties and methods to a constructor function.


How to convert array to JSON in Java? Example Tutorial

Before converting an array to Json. It is good to have a good understanding of an array and what JSon is.  

What then is an Array? An array is a data structure in java that holds values of the same type with its length specified right from creation time. Or you can say an array is a collection of similar type of elements which has contiguous memory location.

Oftentimes you may have to work on a task whereby you will have to store a large number of values during the execution of a program. for instance, you need to read 50 numbers, calculate their average, and find out how many numbers are above the average, and how many numbers are below average.

How to remove a specific item from an array in JavaScript? Example Tutorial

Provided you have an array and you need to remove a specific item, how would you go about it? no worries, there are many ways to kill a bird. So, in JavaScript, there are different methods and techniques you can use to remove element(s) from an array(s). For example, you can use the pop() method to remove the last element of a given array, or you can use the shift() method to remove the first element of a given array. You also have a splice() method which can remove multiple elements from the given array. If you are wondering how to use them then don't worry, you will see examples of all those methods in this article. 

How to append an element to an array in JavaScript? Example Tutorial

Appending means adding an element at the end of the array and before we do that, we need to have a good understanding of an array in javaScript.  What is an array? An array is a single variable that is used to store different elements. This means that JavaScript array(s) can hold different types of data like the number, string, boolean and object values at the same time. Unlike in Java, sorry You can’t do that. It is strongly typed in fact from creation time you would specify what type of data is coming in and its length. And you can’t do otherwise once you have stated the type of data and length any attempts to do that leads to compilation error.

How to remove empty elements in JavaScript array? Example Tutorial

Hello guys, if you are wondering how to remove empty elements from the JavaScript array then you have come to the right place. I have been sharing a lot of Javascript articles lately and this is the new one in the series. In the past, I have shared the best JavaScript courses, books, websites, and Javascript interview questions, along with many JavaScript and React tutorials, and today, we are going to see an example of removing empty elements from the Javascript array.  Actually, there are several ways of removing empty element(s) in javaScript. Removing empty elements connotes that there may be other elements too, and that should make you think of an array. 

How to loop through an array in JavaScript? Example Tutorial

There are several ways of traversing an array in JavaScript. But, before we delve fully into that we need to understand what an array is in JavaScript.

What then is an Array in JavaScript?

An array is a single variable that is used to store different elements. This means that JavaScript array(s) can hold different types of data like the number, string, boolean and object values at the same time.