Facebook
Banner
XMPP JavaScript Library READ MORE

How to use OOPs in JavaScript

JavaScript, Sachin Puri, 2012-04-07 13:32:19

 

This tutorial will give you an example of using OOPs concets in JavaScript. There are many other ways of implementing OOPs in JavaScript but i like this approach most.

        //Class
        function employee(emp_id,emp_name)
        {
            //Class variables
            this.employee_id=emp_id;
            this.employee_name=emp_name;
        }

        //Class Member Functions
        employee.prototype.getDetails=function()
        {
            alert("Employee ID: " + this.employee_id + " Employee Name: " + this.employee_name);
        }

        //Create First Object
        emp1=new employee(1,"Sachin"); //Create object of class        
        emp1.getDetails(); //Call member function of class

        //Create Second Object
        emp2=new employee(2,"Abhinav"); //Create object of class        
        emp2.getDetails(); //Call member function of class

 

Add Your Comment
   
    Yes! I want to receive all comments by email

No Comments Posted Yet. Be the first one to post comment