By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,968 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,968 IT Pros & Developers. It's quick & easy.

setting event to member function

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.