Override method question

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

    #1

    Override method question

    This seems to me like it should work but I get "generic" no matter
    which button I click. Javascript is not one of my better languages.
    Can the experts here take a look at this and tell me what I'm doing
    wrong.

    Thanks, Bob

    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    </HEAD>

    <BODY>
    <div id="zz" />
    <script language="JavaS cript1.2">
    function btn(txt){
    this.click = function(){ alert('generic' );}

    this.b = document.create Element("INPUT" );
    with (this.b){
    type = 'button';
    value = txt;
    onclick = this.click;
    }

    return(this);
    }

    var btnA = new btn('button A');
    document.all.zz .appendChild(bt nA.b);

    var btnB = new btn('button B');
    document.all.zz .appendChild(bt nB.b);

    btnA.prototype. click = function(){ alert('button A pressed');}


    </script>
    <p>Button A should show "button A pressed",<br>
    Button B should show "generic"
    </BODY>
    </HTML>
  • Dom Leonard

    #2
    Re: Override method question

    Bob wrote:
    [color=blue]
    > I get "generic" no matter
    > which button I click.[/color]
    [color=blue]
    >
    > <HTML>
    > <HEAD>
    > <TITLE> New Document </TITLE>
    > </HEAD>
    >
    > <BODY>
    > <div id="zz" />
    > <script language="JavaS cript1.2">[/color]
    the language attribute is deprecated, better:
    <script type="text/javascript">

    FWIW, there is also a bug in javascript 1.2 (function objects do not
    have a prototype object until called with the new operator) that would
    prevent most prototyping code working anyway :)
    [color=blue]
    > function btn(txt){[/color]
    A constructor function[color=blue]
    > this.click = function(){ alert('generic' );}[/color]
    A method of every new btn object (not to be confused with the browser
    supplied click method of form field elements)[color=blue]
    >
    > this.b = document.create Element("INPUT" );
    > with (this.b){[/color]
    warning!!
    The with clause will (likely) create properties on the window object if
    they do not pre-exist as properties of the INPUT element. Consider
    dropping the with clause, although it worked in Mozilla when tested.[color=blue]
    > type = 'button';
    > value = txt;
    > onclick = this.click;[/color]
    'this' refering to the btn object under construction, so the INPUT has
    the onclick method created above: function(){ alert('generic' );}[color=blue]
    > }
    >
    > return(this);[/color]
    (aside: explicit return of the new object is not required. If the
    contstructor does not return an object value, the new operator returns
    the new object anyway)[color=blue]
    > }
    >
    > var btnA = new btn('button A');[/color]
    Okay, INPUT btnA.b has the onclick handler assigned above[color=blue]
    > document.all.zz .appendChild(bt nA.b);[/color]
    (aside: to run in Mozilla
    document.getEle mentById('zz'). appendChild(btn A.b)[color=blue]
    >
    > var btnB = new btn('button B');[/color]
    and btnB.b[color=blue]
    > document.all.zz .appendChild(bt nB.b);[/color]
    (document.getEl ementById('zz') .appendChild(bt nB.b)[color=blue]
    >
    > btnA.prototype. click = function(){ alert('button A pressed');}[/color]

    function objects have a .prototype property for an object data type
    value. btnA is a btn Object, without no property named 'prototype', so
    this line generates javascript errors.[color=blue]
    >
    >
    > </script>
    > <p>Button A should show "button A pressed",<br>[/color]
    Eh, why?[color=blue]
    > Button B should show "generic"
    > </BODY>
    > </HTML>[/color]

    Briefly the prototype of an object (taken from the .prototype property
    of its constructor function at the time of object creation) will supply
    inherited properties to all objects sharing the same prototype object,
    provided individual (instance) object doen't have a local property set
    with the same name.

    FWIW an example with changes noted above and capitalising the first
    letter of the constructor function:


    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    </HEAD>

    <BODY>
    <div id="zz" />
    <script type="text/javascript">

    function Btn(txt, optOnClick){


    this.b = document.create Element("INPUT" );
    this.b.type = 'button';
    this.b.value = txt;
    this.b.onclick = optOnClick || this.click;

    }
    Btn.prototype.c lick = function(){ alert('generic' );}

    var btnA = new Btn('button A',
    function(){ alert('button A pressed')} );

    document.getEle mentById('zz'). appendChild(btn A.b);

    var btnB = new Btn('button B');
    document.getEle mentById('zz'). appendChild(btn B.b);


    </script>
    <p>Button A should show "button A pressed",<br>
    Button B should show "generic"
    </BODY>
    </HTML>

    HTH,
    Dom




    Comment

    • Bob

      #3
      Re: Override method question

      Dom Leonard <doml.removethi s@senet.andthis .com.au> wrote in message news:<OZuab.395 7$vQ1.197587@nn rp1.ozemail.com .au>...[color=blue]
      > Bob wrote:[/color]

      <snip>
      [color=blue]
      > HTH,
      > Dom[/color]

      Thanks for the help Dom, that was exactly what I was looking for.
      Bob

      Comment

      Working...