![]() |
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)For example, in the now defunct event model (meaning that now you typically use addeventlistener), you would have done this: Code:
myDOMNode.onclick = myClickHandler;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”); };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”));onclick(fnc){ } Thanks for response. |
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 |
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”); };********** By the way, the first code example there is wrong: Code:
if (myDOMNode.onclick)Code:
if (myDOMNode.onclick) |
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. |
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() |
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.