473,395 Members | 1,941 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,395 software developers and data experts.

Override method question

Bob
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="JavaScript1.2">
function btn(txt){
this.click = function(){ alert('generic');}

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

return(this);
}

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

var btnB = new btn('button B');
document.all.zz.appendChild(btnB.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>
Jul 20 '05 #1
2 8461
Bob wrote:
I get "generic" no matter
which button I click.
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>

<BODY>
<div id="zz" />
<script language="JavaScript1.2"> 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 :)
function btn(txt){ A constructor function this.click = function(){ alert('generic');} A method of every new btn object (not to be confused with the browser
supplied click method of form field elements)
this.b = document.createElement("INPUT");
with (this.b){ 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. type = 'button';
value = txt;
onclick = this.click; 'this' refering to the btn object under construction, so the INPUT has
the onclick method created above: function(){ alert('generic');} }

return(this); (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) }

var btnA = new btn('button A'); Okay, INPUT btnA.b has the onclick handler assigned above document.all.zz.appendChild(btnA.b); (aside: to run in Mozilla
document.getElementById('zz').appendChild(btnA.b)
var btnB = new btn('button B'); and btnB.b document.all.zz.appendChild(btnB.b); (document.getElementById('zz').appendChild(btnB.b)
btnA.prototype.click = function(){ alert('button A pressed');}
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.

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


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.createElement("INPUT");
this.b.type = 'button';
this.b.value = txt;
this.b.onclick = optOnClick || this.click;

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

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

document.getElementById('zz').appendChild(btnA.b);

var btnB = new Btn('button B');
document.getElementById('zz').appendChild(btnB.b);
</script>
<p>Button A should show "button A pressed",<br>
Button B should show "generic"
</BODY>
</HTML>

HTH,
Dom


Jul 20 '05 #2
Bob
Dom Leonard <do*************@senet.andthis.com.au> wrote in message news:<OZ*******************@nnrp1.ozemail.com.au>. ..
Bob wrote:
<snip>
HTH,
Dom


Thanks for the help Dom, that was exactly what I was looking for.
Bob
Jul 20 '05 #3

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

Similar topics

5
by: Darius | last post by:
I'm writing here in hopes that someone can explain the difference between the new and virtual/override keywords in C#. Specifically, what is the difference between this: public class Window {...
7
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
2
by: Mark Essex | last post by:
I am trying to build an interface that requires the developer to use the OVERRIDE option. I looked at abstract classes, but seemed to have a couple of problems: 1. If you do this in a form, it...
11
by: z_learning_tester | last post by:
Hello, yes another beginner question that I'm sure is obvious to many here :-) My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the...
15
by: John Salerno | last post by:
Hi all. I have a question about virtual and override methods. Please forgive the elementary nature! First off, let me quote Programming in the Key of C#: "Any virtual method overridden with...
1
by: relient | last post by:
I'm learning about the virtual table in association with virtual methods. I got most of the logic and understanding down (I believe) for when you use 'override' and no 'override' or no 'new'...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
4
by: cok | last post by:
Hi, all I have a question about VS 2005 IDE, I donn't know if It is place to post my question,sorry I have a class inherit form System.Windows.Forms.Form, I want to override OnPaint method ,...
13
by: Ben Voigt | last post by:
Is there any way to have an overridden method which is not callable from the assembly overridding it? A contrived example (though I want this feature, my program has nothing to do with food...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.