I'm just trying to understand the logic behind this code:
window.onkeydown = function () {
handler(event);
};
function handler(event)
{
console.log(event.key); // this works!
}
Shouldn't the event handler be declared with the event argument included in the anonymous function? Like so:
window.onkeydown = function (event) {
handler(event);
};
I understand that browsers automatically pass the event to the handler, but still I find it weird that assigning an anonymous function without arguments still works. Is this a normal behaviour?
eventobject as a parameter. Instead, it's a global variable that's updated when an event occurs. That was a horrible design decision but most browsers (Firefox excepted) now imitate that behavior.