JS 1.5 polymorphism

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank

    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.callbackf ctn 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.prot otype = new MyBaseClass;


    function MyBaseClass()
    {

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

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

    }

    var myVar = new MySubClass();

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

    myVar.process() ;

  • Richard Cornford

    #2
    Re: JS 1.5 polymorphism

    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.prot otype = new MyBaseClass;
    >
    >
    function MyBaseClass()
    {
    >
    this.callbackfc tn = function () {
    alert("callback not set by subclass"); };
    >
    this.process = function()
    {
    this.callbackfc tn(); // want this to call the function
    set by the subclass
    };
    >
    }
    >
    var myVar = new MySubClass();
    >
    myVar.callbackf ctn = 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.


    Comment

    • Frank

      #3
      Re: JS 1.5 polymorphism

      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.

      Comment

      • Michael Winter

        #4
        Re: JS 1.5 polymorphism

        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/googlegroupsrep ly/>.

        Comment

        Working...