473,394 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

JS 1.5 polymorphism

For the below code, I am getting a dialog box message with the text
"callback not set by subclass" instead of "my subclass callback
function". Using JS 1.5, is there anyway I can enhance the below code
so that it works the way I want it too? I want to publish these
classes in a .js file and let the developer set myVar.callbackfctn to
be whatever they want it to be for each class instance, and then I want
this.process() to call the callback function that the developer set for
each instance.

I basically want MySubClass() and MyBaseClass() to behave identically
for the users of the library: just two different class names for the
same functionality.

If the developer directly uses new MyBaseClass(), everything works
fine: the code calls the function that the developer assigns to
callbackfctn.

=====

function MySubClass()
{
}

MySubClass.prototype = new MyBaseClass;
function MyBaseClass()
{

this.callbackfctn = function () { alert("callback not set by
subclass"); };

this.process = function()
{
this.callbackfctn(); // want this to call the function set by
the subclass
};

}

var myVar = new MySubClass();

myVar.callbackfctn = function() { alert("my subclass callback
function"); };

myVar.process();

Sep 20 '06 #1
3 1524
Frank wrote:
<snip>
... . Using JS 1.5, is there anyway I can enhance the
below code so that it works the way I want it too?
<snip>
function MySubClass()
{
}

MySubClass.prototype = new MyBaseClass;
function MyBaseClass()
{

this.callbackfctn = function () {
alert("callback not set by subclass"); };

this.process = function()
{
this.callbackfctn(); // want this to call the function
set by the subclass
};

}

var myVar = new MySubClass();

myVar.callbackfctn = function() {
alert("my subclass callback function"); };

myVar.process();
Running this in a browser implementing JavaScript(tm) 1.5 (Mozilla 1.6)
it alerts "my subclass callback function", which is what you say you
want. You will have to go into more details of the environment in which
you are testing this.

Richard.
Sep 21 '06 #2
I created a simple example to post the question and the simple example
works in my browser too.

I need to start throwing out things in the code that is not working to
locate what is causing the difference in behavior.

Sep 21 '06 #3
Frank wrote:
I created a simple example to post the question and the simple example
works in my browser too.
Then why post only that? We can't help you fix broken code if you don't
post relevant material.

[snip]

Mike
Please quote what you are replying to when posting to this group. See
<http://www.safalra.com/special/googlegroupsreply/>.
Sep 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.