var btn = document.getElementById('button');
btn.addEventListener('click', function(e) {
// 处理兼容性
e = e || window.event;
e.target = e.target || e.srcElement;
e.preventDefault = e.preventDefault || function() {
e.returnValue = false;
};
e.stopPropagation = e.stopPropagation || function() {
e.cancelBubble=true;
};
// 业务逻辑
console.log('处理业务逻辑');
});
var btn = document.getElementById('button');
btn.addEventListener('click', function(e) {
// 处理兼容性
dealEvent(e);
// 业务逻辑
console.log('处理业务逻辑');
});
function dealEvent(e) {
e = e || window.event;
e.target = e.target || e.srcElement;
e.preventDefault = e.preventDefault || function() {
e.returnValue = false;
};
e.stopPropagation = e.stopPropagation || function() {
e.cancelBubble=true;
};
}
btn.use('click', function(e, next) {
e = e || window.event;
e.target = e.target || e.srcElement;
e.preventDefault = e.preventDefault || function() {
e.returnValue = false;
};
e.stopPropagation = e.stopPropagation || function() {
e.cancelBubble=true;
};
next();// 不调用next就不会继续匹配下面的click
});
btn.addEventListener('click', function(e, next) {
cosole.log('111业务逻辑111'); // 此时调用e,已经不存在兼容问题, 代码非常养眼
});
btn.addEventListener('click', function(e, next) {
cosole.log('222业务逻辑222'); // 此时调用e,已经不存在兼容问题, 代码非常养眼
});
//////////////////////////////////////////////////////////////////////////////////////////
function emitterClass() {
this._events = {}
}
emitterClass.prototype.on = function(type, fn) {
this._events[type] = this._events[type] || [];
if (this._events[type].indexOf(fn) === -1) {
this._events[type].push(fn);
}
}
emitterClass.prototype.emit = function(type) {
var self = this, args = [].slice.call(arguments, 1);
this._events[type] = this._events[type] || [];
args.push(next);
var fn = self._events[type].shift();
fn.apply(self, args);
function next() {
var fn = self._events[type].shift();
if (arguments.length !== 0) {
fn.apply(self, arguments);
}
else {
fn.apply(self, args);
}
}
}
var obj = new emitterClass();
obj.on('customEvent', function(data, next) {
data = '封装[' + data +']';
next(data, next);
});
obj.on('customEvent', function(data, next) {
console.log(data);
next(data, next);
});
obj.on('customEvent', function(data, next) {
console.log(data);
});
obj.emit('customEvent', "原始数据");
obj.emit('customEvent', "原始数据");// error 只能调用一次
我希望的api是酱紫的:
obj.addFilter('customEvent', function(data, next) {
data = '封装[' + data +']';
next(data, next);
});
obj.on('customEvent', function(data) {
console.log(data);
});
obj.on('customEvent', function(data) {
console.log(data);
});
obj.emit('customEvent', "原始数据");
obj.emit('customEvent', "原始数据");// 可以多次调用
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我用 ES6,以你的思路为基础写了一个 Emitter。注意这里把中间件和事件处理区分开了(你的是没区分开的)。中间件是在事件处理函数之前进行调用,对事件对象进行一些提前操作。完成之后进入事件处理环节,这个时候所有处理函数得到的事件对象应该都是同一个。
可以仿jq的嘛,把所有事件回调都用队列存起来。
addEventListener里面绑一个函数,这个函数先处理兼容问题,之后提取出你之前存起来的回调队列,最后再挨着运行。在回调里面加入
next来控制流程,又或者是像koa那样直接跳转到不同的回调中都是很容易实现的。看了下你的代码,你on加的3个事件都被shift()抽干净了,第二次里的应该已经没有函数了吧。。所以会报错啊。。
偶觉得,ES7 的Decorator有可能可以为君解忧。。。