Hello All, We are going to start new batch from next week. message/call or mail us for more details.

14 August 2014

OOPS in javascript - DotNet Brother

Introduction
Javascript is Prototype-based programming and a style of object-oriented programming in which classes are not present. It can support oops because it support 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");

1 comment: