javascript publish subscribe

Publish-Subscribe属于观察着模式,在很多框架上都用来解耦。

下面,我用JavaScript来实现一个简单的Demo。

PubSub = {handlers: {}};

PubSub.on = function(eventType, handler) {
	if (!(eventType in this.handlers)) {
		this.handlers[eventType] = [];
	}
	this.handlers[eventType].push(handler);
	return this;
}

PubSub.emit = function(eventType) {
	var handlerArgs = Array.prototype.slice.call(arguments, 1);
	for (var i = 0; i < this.handlers[eventType].length; i++) {
		this.handlers[eventType][i].apply(this, handlerArgs);
	}
	return this; 
}

PubSub.on('click',function(e){
	console.log('1',e);
}).on('click',function(e){
	console.log('2',e);
}).on('click',function(e){
	console.log('3',e);
});

PubSub.emit('click','hello');