How to implement inheritance in JavaScript (without using JavaScript's "Prototype")

by Jagadish Pulakhandam on 9/14/2012 10:54:23 AM
Rated 0 from 0 votes
Brief: Demonstrates on implementing a simple inheritance without using "prototype" in JavaScript
Posted to: Advanced JavaScript Programming/Development tutorials/videos- Object Oriented Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached Source Code/video demonstrates the following:
  • How to implement Simple Inheritance in JavaScript without using "prototype" 
  • How to create instances of child class in JavaScript
  • How to access members from child object
Source Code

01.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
03.<html>
04.<head>
05.    <title></title>
06.    <script type="text/javascript">
07.        //DEMO: Simple Inheritance (without using Prototype - a work around)
08. 
09.        //create parent class
10.        var Person = function (vId, vName) {
11.            this.Id = vId;
12.            this.Name = vName;
13. 
14.            this.Display = function () {
15.                alert("Id=" + this.Id);
16.            }
17.        };
18. 
19.        //create new class
20.        var Emp = function (vId, vName, vOfficeMail) {
21.            Person.call(this, vId, vName)
22.            this.OfficeEmail = vOfficeMail;
23.        };
24. 
25.        //create instance of child class
26.        var oEmp = new Emp(1001, "Jag", "a@a.com"); //using Child's constructor
27. 
28.        //call display method in parent class
29.        oEmp.Display();
30. 
31. 
32.        alert(oEmp.hasOwnProperty('OfficeEmail'));//true
33.        alert(oEmp.hasOwnProperty('Id'));//true
34.        alert(oEmp.hasOwnProperty('Name'));//true
35.        alert("Id" in oEmp); //true - inherited property
36.        alert("Name" in oEmp); //true - inherited property
37.        alert("OfficeEmail" in oEmp); //true - direct property
38.    </script>
39.</head>
40.<body>
41. 
42.</body>
43.</html>

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following