Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I override a base class method in Javascript

Posted on 2008-06-24
7
Medium Priority
?
567 Views
Last Modified: 2012-08-14
I have a base class defined as follows
BaseClass = function(id)
{
this.ID = document.getElementById(id);
this.Show() = function() {
document.getElementById(this.ID).style.display = 'block';
};
}
and an inherited class defined as follows

Label = function(id)
{
this.inheritFrom = BaseClass;
this.inheritFrom(id);
}
I want to introduce a Show method on the Label class so that it overrides the base class method, calls the base class method and adds it's own extra implementation for this method.  How can I do this?
Thanks
0
Comment
Question by:kayhustle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21863911
use the "super" "object" to call the inherited classes method.
0
 
LVL 1

Author Comment

by:kayhustle
ID: 21865115
How do I do that?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21865215
this should explain it all:
http://phrogz.net/JS/Classes/OOPinJS2.html
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:kayhustle
ID: 21867323
Can you give me a code example, not sure I follow your link.
0
 
LVL 1

Author Comment

by:kayhustle
ID: 21867764
Yeah I tried about 4 different variations of what was given in the article and none of them worked.
0
 
LVL 16

Accepted Solution

by:
Lolly-Ink earned 2000 total points
ID: 21870721
Here is a working example based on your original code. There were some errors in BaseClass which have been fixed:
<html>
<script>
   BaseClass = function(id)
   {
      this.id = id;
      this.Show = function()
      { 
         document.getElementById(this.id).style.display = 'block';
      }
   }
 
   Label = function(id)
   {
      this.inheritFrom = BaseClass;
      this.inheritFrom(id);
      this._Show = this.Show;
      this.Show = function()
      {
         this._Show();
         document.getElementById(this.id).style.backgroundColor = 'red';
      }
   }
 
   var one = new BaseClass("spanOne");
   var two = new Label("spanTwo");
 
   function load()
   {
      window.setTimeout("changeSpans()", 2000);
   }
 
   function changeSpans()
   {
      one.Show();
      two.Show();
   }
</script>
<body onload="load()">
   <span id="spanOne">One</span>
   <span id="spanTwo">Two</span>
</body>
</html>

Open in new window

0
 
LVL 1

Author Closing Comment

by:kayhustle
ID: 31470352
Perfect!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question