Home > DeveloperSection > Articles > Object Oriented Javascript

Object Oriented Javascript


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 3862  View(s)
Rate this:

Object Oriented Javascript

Introduction

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.

  1. 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");

               alert(Emp1.firstname);

      

 

ii)                   Snap for defining properties

Object Oriented Javascript

 

Object Oriented Javascript

 

iii)                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");

 

 

Conclusion

 

This document contains basics of OOP in javascript


Don't want to miss updates? Please click the below button!

Follow MindStick