Month End Sale: Get Extra 10% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Understanding Prototype in JavaScript

Understanding Prototype in JavaScript

01 Feb 2024
Intermediate
3.57K Views
2 min read
Learn via Video Course & by Doing Hands-on Labs

JavaScript For Beginners Free Course

JavaScript is a prototype-based programming language which has no class like as C++, C#, Java etc. JavaScript uses functions as classes. Hence, in JavaScript we can define a class name Student as given below-

<script>
function Student() { }

//creating instances of class
var st1 = new Student();
var st2 = new Student(); 
</script>

Read More: 50+ Javascript Interview Questions

Making members public and private

You can define a private or local variable inside a class by using var keyword. When you define a variable without var keyword inside a class, it acts as a public variable.

<script>
//Person class
var Person = function () {
 var id; //private
 var show = function () { }; //private function

 this.Name = "Deepak"; //public
 this.Address = "Dehi"; //public
 this.Display = function () { //public function
 //do something
 };
};

//creating instance of Person class
var person1= new Person();
 
// calling the person class Display method.
person1.Display();
</script>

Prototype-based programming

Prototype-based programming is a style of object-oriented programming in which classes are not present, and code re-usability or inheritance is achieved by decorating existing objects which act as prototypes. This programming style is also known as class-less, prototype-oriented, or instance-based programming.

Read More: Javascript Developer Salary in India

Defining constructor in JavaScript

In JavaScript, the function acts as the constructor for the instance. Function within JavaScript is defined by using function keyword followed by parentheses (). You can define a Person class with constructor as given below:

<script>
var Person=function (firstname, lastname, age) { //constructor
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
};

var person1 = new Person("Deepak", "Kumar", 50);
</script>

Different ways to create object/instance

In JavaScript, you can create object in two different ways as given below-

  1. Using Constructor function

    You can create the object in JavaScript, by using new keyword and constructor function as given below-

    <script>
    function Person(firstname, lastname, age) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.age = age;
    }
    
    var person1 = new Person("Deepak", "Kumar", 50);
    var person2 = new Person("Manu", "chauhan", 21);
    </script>
    
  2. Using Object initializer

    You can also create the object in JavaScript, by using object initializer as given below-

    <script>
    person1 = new Object();
    person.firstname = "Deepak";
    person.lastname = "Kumar";
    person.age = 50;
    
    //Or
    person1 = { firstname: "Deepak", lastname: "Kumar", age: 50 };
    </script>
    
What do you think?

I hope you will enjoy the prototype while programming with JavaScript. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Take our Javascript skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Azure Developer Certification Training Jul 28 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Data Structures and Algorithms Training with C# Jul 28 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Microsoft Azure Cloud Architect Aug 11 SAT, SUN
Filling Fast
03:00PM to 05:00PM (IST)
Get Details
Angular Certification Course Aug 11 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this