Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-13-2010, 09:01 PM   PM User | #1
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
JavaScript events are really just function references?

Hey all,

I was reading a book and guy says an event is just a function reference, so you can programmatically call it like a function :
Code:
if (myDOMNode.onclick)
myDOMNode.onclick();
Here's my thing with this. How does the javascript engine know what function reference that this function reference is pointing to?

For example, in the now defunct event model (meaning that now you typically use addeventlistener), you would have done this:
Code:
myDOMNode.onclick = myClickHandler;
So obviously, myClickHandler is just an identifier pointing to a function, because you wouldn't want to call the function directly: myClickHandler() - otherwise interpreter will read that and execute it before it even sees what's on the left side of assignment.

But how is javascript grabbing that myClickHandler and doing something with it if all onclick is is a function reference?

Let's say you have this:
Code:
document.getElementById("bla").onclick = function(e) { myClickHandler(e, “John Merlino”); };
what is javascript really doing with onclick and the anonymous function?

For example, it's not like we are passing the anonymous function as an argument of onclick:
Code:
document.getElementById("bla").onclick({ myClickHandler(e, “John Merlino”));
I'm sure there's not a built in method like this in the engine:
onclick(fnc){
}



Thanks for response.
johnmerlino is offline   Reply With Quote
Old 11-13-2010, 09:23 PM   PM User | #2
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,233
Thanks: 11
Thanked 157 Times in 157 Posts
DrDOS is infamous around these parts
Because that way of incorporating an event handler automatically includes the 'this' functionality of javascript. It's a much better method than the inline.

http://www.quirksmode.org/js/this.html
DrDOS is online now   Reply With Quote
Users who have thanked DrDOS for this post:
johnmerlino (11-20-2010)
Old 11-14-2010, 12:19 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,567
Thanks: 62
Thanked 4,058 Times in 4,027 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
not true. Or at least half-true.

If you want to pass "this" to the anonymous function, you *must* do so yourself.

Example:
Code:
document.getElementById("bla").onclick = function(e) { myClickHandler(e, this, “John Merlino”); };
Now, having said that, it's really not necessary to pass this as you can always get the object that caused the event from the event object. But it's often more convenient to do so, as if you use the event object you have to write browser-sensitive code.

**********

By the way, the first code example there is wrong:
Code:
if (myDOMNode.onclick)
    myDOMNode.onclick();
It would normally be:
Code:
if (myDOMNode.onclick)
    myDOMNode.click();
That is, you simulate the *event* and that invokes the function that is referenced by the event *handler*.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
johnmerlino (11-20-2010)
Old 11-14-2010, 12:25 AM   PM User | #4
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 710
Thanks: 31
Thanked 128 Times in 119 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
I think you and the author meant that an event handler is a function reference.

The event itself is defined and supplied by the interface of the client (browser) and operating system.
mrhoo is offline   Reply With Quote
Old 11-14-2010, 12:27 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,567
Thanks: 62
Thanked 4,058 Times in 4,027 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yes, well said, Hoo. And if you think of it that way, you won't make the mistake of trying to invoke the event handler by doing
Code:
     objectReference.onclick()
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-20-2010, 01:13 AM   PM User | #6
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
Thanks links were helpful.
johnmerlino is offline   Reply With Quote
Reply

Bookmarks

Tags
event, function reference, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 10:52 PM.


Advertisement
Log in to turn off these ads.