首页 > 其他 > 详细

addEventListener之listener obj

时间:2015-09-26 15:50:16      阅读:155      评论:0      收藏:0      [点我收藏+]

今天在学习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
The object that receives a notification when an event of the specified type occurs. This must be an object implementing theEventListener 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);

addEventListener之listener obj

原文:http://www.cnblogs.com/walle2/p/4840760.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!