Javascript is Prototype-based programming is a style of object-oriented

programming in which classes are not present.

It can support OOP because it supports inheritance through prototyping as well as

properties and methods

Object oriented programming in JS know as Prototype-based programming 

a.    Creating Class in javascript

Following syntax is used for declaring class in JS

function Emp() {
    alert('Emp instantiated'); 

Here Emp can be act as Class in JS

Body of Emp act as constructor as called as soon we create object of class.

Creating Objects of Emp Class

Using following syntax we can creating object of Emp class

var Emp1 = new Emp();

As soon we create object of Emp class it constructor we be called

Here above function Emp would be treated as class in JS

Object Oriented Javascript 

c.       Running the code 

Object Oriented Javascript 

Alert will be shown on page load of web form.  

d.      Properties of class

We can define properties of class in following way 

function Emp(firstname) {
  alert('Emp instantiated');
   this.firstname = firstname;   

i)    Passing value to properties

   var Emp1 = new Emp("Devesh is Emp of GENPACT INDIA");

ii)     Snap for defining properties

Object Oriented Javascript 

Object Oriented Javascript 

ii)     Complete code

Object Oriented Javascript 

iv)                Running code 

Object Oriented Javascript

When we run the code we got alert of the string which we are passing to class of Emp.

  var Emp1 = new Emp("Devesh is Emp of GENPACT INDIA");


This document contains basics of OOP in javascript

  Modified On Nov-29-2019 11:39:25 PM

Leave Comment