473,971 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting event to member function

I've been working on improving my javascript coding and decided to
start using classes. I've gotten pretty far but am having problems
attaching a member function to a control event.

CBonfireStore.p rototype.setupC lick = function( obj )
{
obj.onclick = function(){ this.incrementC licked(obj); };
obj.style.curso r = 'pointer';
}

The above does not work. If I take away the this. infrom of the
incrementClicke d(obj), and point to a non-member version of the
function, every works OK.... but that's not what I want. I do I get
this to use the method of the current instance?

John
Nov 17 '07 #1
2 1643
br******@gmail. com wrote:
I've been working on improving my javascript coding and decided to
start using classes. I've gotten pretty far but am having problems
attaching a member function to a control event.

CBonfireStore.p rototype.setupC lick = function( obj )
{
obj.onclick = function(){ this.incrementC licked(obj); };
obj.style.curso r = 'pointer';
}

The above does not work.
this inside of the anonymous function is the obj you set the onclick
property on. If you want the original this you can do e.g.

CBonfireStore.p rototype.setupC lick = function( obj )
{
var thisStore = this;
obj.onclick = function(){ thisStore.incre mentClicked(obj ); };
obj.style.curso r = 'pointer';
};
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 17 '07 #2
br******@gmail. com wrote:
I've been working on improving my javascript coding and decided to
start using classes.
But you could not and did not, as the languages you are using have no
concept of classes. They are object-oriented programming languages using
prototype-based inheritance:

http://javascript.crockford.com/javascript.html
I've gotten pretty far but am having problems
attaching a member function to a control event.
"Member" is a term from class-based thinking; it does not apply here.
CBonfireStore.p rototype.setupC lick = function( obj )
You should remove the leading `C' if it is only there to indicate that
`CBonfireStore' was a class and would therefore promote the misconception
of the existence of classes in the used languages to the uninitiated developer.

Usually constructors for a prototype object, which is what `CBonfireStore'
actually refers to, have reference identifiers starting with a capital
letter to distinguish them from other identifiers, and in that sense
`BonfireStore' would suffice.
{
obj.onclick = function(){ this.incrementC licked(obj); };
Use DOM Level 2 Event methods to assign event listeners, and proprietary
approaches only when necessary. See also _addEventListen er() in
http://PointedEars.de/scripts/dhtml.js (updated recently, see
http://PointedEars.de/scripts/dhtml.js.diff)
obj.style.curso r = 'pointer';
}
You should use the more interoperable spaces for indentation, not tabs.
The above does not work.
http://www.jibbering.com/faq/faq_not...ml#ps1DontWork
If I take away the this. infrom of the incrementClicke d(obj),
and point to a non-member version of the function,
It is also a misconception that globally declared functions would not be
methods; they are methods of the Global Object which is found through
identifier resolution along the scope chain.
every works OK.... but that's not what I want.
It would appear that the problem is in incrementClicke d() which you have not
posted.
I do I get this to use the method of the current instance?
This sentence sense.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8************ *******@news.de mon.co.uk>
Nov 17 '07 #3

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

Similar topics

2
11139
by: Hasan Ammar | last post by:
Is it possible to set up hotkeys using onkeypress? I know it can be done with the usual alphanumeric keys, but what about function keys? or using ctrl/alt combinations? Does anybody have a tutorial/guide?
18
18496
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on a remote update of tables and fields and can't find enough information on these things. Also, how do you index a field in code?
1
1350
by: Jonel | last post by:
Hi, let's say i have a class with a custom event and it has one member function that i ran asynchronously and that member function raises the custom event, would any class that subscribe in that event receive a notification from that member function even tho it was run asynchronously? thanks, Jonel
8
2279
by: David Lozzi | last post by:
Howdy, I have a user control that is a report to display data. On the page the control is inserted in, I have filter options to filter the report. When I try to do something like this, nothing happens. dim filt as string ... build filter string... UserControl.ReportFilter = filt
0
2669
by: David J | last post by:
Hi, I am strugling with the propertygrid and a listbox. I am using the universaldropdowneditor from the codeproject (code below). However I am populating the listbox via a datasource. The problem I am having is that when I have a value in the propertygird and edit that, I want the listbox to have the selectvalue equal to the value that is being edited. Just to make it clearer: PropgridVal = Germany Datasource=
8
1897
by: wASP | last post by:
Hi, I'm having a problem referencing the elements within an object after a method of that object (a member function) has been activated with an onsubmit handler: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <script language="javascript" type="text/javascript"> function js_onsubmit_hndl_fn () {
8
1522
by: pauldepstein | last post by:
I am writing a program which looks at nodes which have coordinates which are time-dependent. So I have a class called node which contains the private member declarations int date; int month; (I want to consider the month as part of the information contained in the node, as well as the date.) The node class also contains public member function void set_month(int) which sets the month according to the date given.
2
3741
by: Mayur | last post by:
Hello all, Can any one have the source of How to add event handler in unmanaged MFC application for event source which is in Managed(c# class library). Here is the Event Source code : namespace ircConnect {
6
2641
by: TriFuFoos | last post by:
Hi there, I was wondering if anyone knew if/how to assign an event to a global variable? I tried to do it and IE 7 came back with an error saying "Member not found" My code looked similar to the following: var globalEvevnt; function showPopup(event){ globalEvent = event;
0
10346
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10159
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11805
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11555
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10900
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10067
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
5143
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.