CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   JavaScript events are really just function references? (http://www.codingforums.com/showthread.php?t=209224)

johnmerlino 11-13-2010 09:01 PM

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.

DrDOS 11-13-2010 09:23 PM

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

Old Pedant 11-14-2010 12:19 AM

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*.

mrhoo 11-14-2010 12:25 AM

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.

Old Pedant 11-14-2010 12:27 AM

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()

johnmerlino 11-20-2010 01:13 AM

Thanks links were helpful.


All times are GMT +1. The time now is 12:42 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.