kiinlam
4/18/2016 - 9:28 AM

addEventListener usage

addEventListener usage

// addEventListener usage

var o = {
  handleEvent: function(e){
    this[e.type + 'Event'](e);
  },
  clickEvent: function (e) {
    console.log(e.type);
  },
  mouseenterEvent: function (e) {
    console.log(e.type);
  },
  mouseoutEvent: function (e) {
    console.log(e.type);
  }
};

// usage 1
// addEventListener(eventType, obj)
// obj implement: {handleEvent: function}
elem.addEventListener('click',      o);
elem.addEventListener('mouseenter', o);
elem.addEventListener('mouseout',   o);

// usage 2
// addEventListener(eventType, function)
elem.addEventListener('click',      o.clickEvent);
elem.addEventListener('mouseenter', o.mouseenterEvent);
elem.addEventListener('mouseout',   o.mouseoutEvent);