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

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.prototype.setupClick = function( obj )
{
obj.onclick = function(){ this.incrementClicked(obj); };
obj.style.cursor = 'pointer';
}

The above does not work. If I take away the this. infrom of the
incrementClicked(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 1616
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.prototype.setupClick = function( obj )
{
obj.onclick = function(){ this.incrementClicked(obj); };
obj.style.cursor = '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.prototype.setupClick = function( obj )
{
var thisStore = this;
obj.onclick = function(){ thisStore.incrementClicked(obj); };
obj.style.cursor = '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.prototype.setupClick = 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.incrementClicked(obj); };
Use DOM Level 2 Event methods to assign event listeners, and proprietary
approaches only when necessary. See also _addEventListener() in
http://PointedEars.de/scripts/dhtml.js (updated recently, see
http://PointedEars.de/scripts/dhtml.js.diff)
obj.style.cursor = '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 incrementClicked(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 incrementClicked() 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.demon.co.uk>
Nov 17 '07 #3

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

Similar topics

2
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...
18
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...
1
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...
8
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...
0
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...
8
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: - - - - - - - - ...
8
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; ...
2
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 :...
6
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.