While implementing a closure function, I have noticed that if I provide a "named function" as an event handler then it gets executed straightaway when the event gets attached to the buttons. However, if I keep the function inline as an anonymous function then it doesn't execute straightaway and fires only on the event happens. Please can anyone explain this behaviour?
var buttons = document.getElementsByTagName('button');
//function buttonHandler(buttonName){
////  return function(){
////    console.log(buttonName);
//// }
//  alert("hello");
//}
var buttonHandler = function(name){
  alert(name);
}
for(var i = 0; i < buttons.length; i += 1) {
 var button = buttons[i];
 var buttonName = button.innerHTML;
 button.addEventListener('click', buttonHandler(buttonName));
  button.addEventListener('click', function(buttonName){
          alert("hi");
      });
}Many Thanks!
 
    