今天在学习iscroll源码的时候,发现它的addEventListener函数第二个事件传入的是 IScroll 这个对象,而不是一个函数。
// this 就是 IScroll 对象 eventType(this.wrapper, ‘touchstart‘, this); eventType(target, ‘touchmove‘, this); eventType(target, ‘touchcancel‘, this); eventType(target, ‘touchend‘, this);
谷歌之,MDN上就有相关介绍,嘿嘿
关于第二个参数 listener, 有下面一句话:
listener
EventListener
interface, or simply a JavaScript function.四级水平翻译下: listener 是当有特定类型的事件触发时被通知的那个对象,该对象必须是一个实现EventListener接口的对象,或者是一个js函数.
如果传递的是一个对象,当有事件触发时,会调用对象下面的 handleEvent 方法
var obj = { name: ‘foo‘, handleEvent: function (ev){ switch(ev.type){ case ‘click‘: this.clickHandler(); break; case ‘mousedown‘: this.mousedownHandler(); break; case ‘mouseup‘: this.mouseupHandler(); break; } }, clickHandler: function (){ console.log(‘click‘); }, mousedownHandler: function (){ console.log(‘mousedown‘); }, mouseupHandler: function (){ console.log(‘mouseup‘); } }; document.addEventListener(‘click‘, obj, false); document.addEventListener(‘mousedown‘, obj, false); document.addEventListener(‘mouseup‘, obj, false);
原文:http://www.cnblogs.com/walle2/p/4840760.html