I use event delegation in such way:
elWraper.onclick = (function(){
//how to get here "event" object
e = e || window.event;
var t = e.target || e.srcElement;
//event handler
return function(){
//manipulations with "t" variable
}
})();
how to get “event” object within the immediately executed function?
In standards compliant browsers the event object is the first parameter passed into the callback function. In IE it is a global variable (which is what
e = e || window.eventis trying to determine). Therefore, the function that you return by theimmediately executed functionshould accept the event object declared as its first (and usually only) argument.Clarification
Since people are wondering (and probably they are wondering why the OP accepted this answer) there are uses for this that is not clear from the OP’s question. One is to create a closure to a variable to track something:
also, this is the classic way of assigning event handlers in loops:
Or maybe the OP just thought that he could ‘cache’ the event object and mistakenly believed he could use this to do it. In which case, reading my explanation (instead of just the code) should enlighten the reader as to why that would be a bad idea.