A step to share new things

Classes In Javascript

In JavaScript there are no classes but Functions can be used to somewhat simulate classes. Everything is an object in JavaScript so Inheritance in JavaScript means objects inherit from objects, not classes from.

Ways to Implement Classes in JavaScript:

  1. We can define a normal JavaScript function and then create an object by using the new keyword. To define properties and methods use the this keyword.

Example 1:-

<script type=”text/javascript”>
function Employee(){
var empID = 1142;
this.empFirstName = “Abhishek”;
this.empLastName = “Gupta”;
this.getEmployeeDetail = function(){
alert(“EmployeeID : ” + empID + ” Employee Name : ” +
this.empFirstName + ” ” + this.empLastName);
};
}

function main(){
var objEmployee = new Employee();
objEmployee.getEmployeeDetail();  //EmployeeID : 1142
// Employee Name : Abhishek Gupta
alert(objEmployee.empFirstName); //Abhishek
alert(objEmployee.empLastName);  //Gupta
alert(objEmployee.empID);        //undefined because empID is private    variable.
}
</script>

HTML Code :
<div>
<asp:Button ID=”btnCallmain” runat=”server” Text=”CallMain”
OnClientClick=”javascript:main();”/>
</div>

Example 2 :-

<script type=”text/javascript”>

function Employee(id,fname,lname){
var empID=id;
var empFName = fname;
var empLName = lname;
this.showEmployeeDetail = function(){
alert(“EmployeeID : ” + empID + ” Employee Name : ” + empFName + ” ” +
empLName);
}
}

function main(){
var objEmployee = new Employee(5,”Abhishek”,”Gupta”);
objEmployee.showEmployeeDetail();
}
</script>

HTML Code :

<div>
<asp:Button ID=”btnCallmain” runat=”server” Text=”CallMain”
OnClientClick=”javascript:main();”/>
</div>

  1. We can Create Class using JSON too. While we use JSON to create class then Object is also initiated at that same time, we don’t have to create object of that class separately.

    Example 1:-

    <script type=”text/javascript”>
    var Employee ={
    “empID”:””,
    “empFirstName”:””,
    “empLastName”:””,
    “setValues”:function(id,fname,lname){
    this.empID = id;
    this.empFirstName = fname;
    this.empLastName = lname;
    },
    “getValues”:function(){
    alert(this.empID + ” ” + this.empFirstName + ” ” +
    this.empLastName);
    }
    }

    function main(){
    Employee.setValues(“1142″,”Abhishek”,”Gupta”);
    Employee.getValues();
    }

    </script>

    HTML Code :

    <div>
    <asp:Button ID=”btnCallmain” runat=”server” Text=”CallMain”
    OnClientClick=”javascript:main();”/>
    </div>

    Inheritance in JavaScript

    function Employee(){
    var empID;
    var empAge;
    var empName;
    this.setEmployee = function(id,age,name){
    empID = id;
    empAge = age;
    empName = name;
    };
    this.getEmployee = function(){
    return “Employeee Name : ” + empName + “, EmployeeID :” + empID + “,
    Employee Age : ” + empAge;
    }
    }

    function Departmnt(){
    this.inheritFrom = Employee;
    this.inheritFrom();
    var departmentID;
    var departmentName;
    this.setDepartment = function(depID,depName){
    departmentID = depID;
    departmentName= depName;
    };
    this.getWholeEmpDetail = function(){
    alert(this.getEmployee() + ” DepartmntID : ” + departmentID + “,
    Department Name : ” + departmentName);
    };
    }

    function main(){
    var objDepartment = new Departmnt();
    objDepartment.setEmployee(1142,27,”Abhishek Gupta”);
    objDepartment.setDepartment(“IT_DEV”,”Development”);
    objDepartment.getWholeEmpDetail();
    }

    HTML Code :

    <div>
    <asp:Button ID=”btnCallmain” runat=”server” Text=”CallMain”
    OnClientClick=”javascript:main();”/>
    </div>

    Advertisements

    Comments on: "Classes, Inheritance in JavaScript" (6)

    1. […] Classes, Inheritance in JavaScript (via Abhishek Gupta) Classes In Javascript In JavaScript there are no classes but Functions can be used to somewhat simulate classes. Everything is an object in JavaScript so Inheritance in JavaScript means objects inherit from objects, not classes from. Ways to Implement Classes in JavaScript: We can define a normal JavaScript function and then create an object by using the new keyword. To define properties and methods use the this keyword. Example 1:- <script ty … Read More […]

    2. […] Classes, Inheritance in JavaScript (via Abhishek Gupta) Posted on January 14, 2011 by 🙂 Classes In Javascript In JavaScript there are no classes but Functions can be used to somewhat simulate classes. Everything is an object in JavaScript so Inheritance in JavaScript means objects inherit from objects, not classes from. Ways to Implement Classes in JavaScript: We can define a normal JavaScript function and then create an object by using the new keyword. To define properties and methods use the this keyword. Example 1:- <script ty … Read More […]

    3. […] Classes In Javascript In JavaScript there are no classes but Functions can be used to somewhat simulate classes. Everything is an object in JavaScript so Inheritance in JavaScript means objects inherit from objects, not classes from. Ways to Implement Classes in JavaScript: We can define a normal JavaScript function and then create an object by using the new keyword. To define properties and methods use the this keyword. Example 1:- <script ty … Read More […]

    4. thanks 🙂 its nice!

    5. […] Classes, Inheritance in JavaScript (via Abhishek Gupta) Posted on January 14, 2011 by deepssharma Classes In Javascript In JavaScript there are no classes but Functions can be used to somewhat simulate classes. Everything is an object in JavaScript so Inheritance in JavaScript means objects inherit from objects, not classes from. Ways to Implement Classes in JavaScript: We can define a normal JavaScript function and then create an object by using the new keyword. To define properties and methods use the this keyword. Example 1:- <script ty … Read More […]

    6. […] Classes, Inheritance in JavaScript (via Abhishek Gupta) Posted on January 14, 2011 by Neha Bhargava Classes In Javascript In JavaScript there are no classes but Functions can be used to somewhat simulate classes. Everything is an object in JavaScript so Inheritance in JavaScript means objects inherit from objects, not classes from. Ways to Implement Classes in JavaScript: We can define a normal JavaScript function and then create an object by using the new keyword. To define properties and methods use the this keyword. Example 1:- <script ty … Read More […]

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    Tag Cloud

    %d bloggers like this: