johnmerlino
11-13-2010, 09:01 PM
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 :
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:
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:
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:
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.
I was reading a book and guy says an event is just a function reference, so you can programmatically call it like a function :
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:
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:
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:
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.