By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

JS 1.5 polymorphism

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.