How to override methods in JavaScript OOPS (without using "prototype")

by Jagadish Pulakhandam on 9/18/2012 5:06:32 PM
Rated 0 from 0 votes
Brief: Demonstrates on overriding methods using JavaScript without using "prototype"
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 create parent/child classes (function constructors) in JavaScript without using "prototype"
  • How to declare methods in parent class and override the same in child class
  • How to create instances/objects of parent/child classes and access the overridden methods
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: Overriding methods (without using Prototype)
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.toString() +
16.                        ", Name=" + this.Name);
17.            }
18.        };
19. 
20.        //create new class
21.        var Emp = function (vId, vName, vOfficeMail) {
22.            Person.call(this, vId, vName)
23.            this.OfficeEmail = vOfficeMail;
24.            //overriding method
25.            this.Display = function () {
26.                alert("Id=" + this.Id.toString() +
27.                        ", Name=" + this.Name +
28.                        ", OfficeMail=" + this.OfficeEmail);
29.            }
30.        };
31. 
32.        //create instance of parent class
33.        var oPerson = new Person(1002, "Chat"); //using Parent's constructor
34.        //call display method in parent class
35.        oPerson.Display();
36. 
37.        //create instance of child class
38.        var oEmp = new Emp(1001, "Jag", "a@a.com"); //using Child's constructor
39.        //call display method in child class
40.        oEmp.Display();
41. 
42. 
43. 
44.    </script>
45.</head>
46.<body>
47. 
48.</body>
49.</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