Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > setting event to member function

Reply
Thread Tools

setting event to member function

 
 
bruestle@gmail.com
Guest
Posts: n/a
 
      11-17-2007
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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      11-17-2007
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/
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-17-2007
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, <f806at$ail$1$>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM
Function pointer member variable to non-member function Alex C++ 0 10-15-2003 05:26 PM
Function pointer member variable to non-member function slide_o_mix C++ 0 10-15-2003 03:37 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57