setting event to member function

B

bruestle

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
 
M

Martin Honnen

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';
};
 
T

Thomas 'PointedEars' Lahn

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_notes/clj_posts.html#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top