标签:style blog io ar color sp for on div
1.观测者模式;
有时候被称为发布订阅模式,定义了一种一对多的依赖关系,让多个观察者同时监听同一主题对象,这个主题对象在发生变化时会通知观察者,使它们可以更新自己;
var Obsever = {};
//添加观察者; var add = function (name, callback){ if(typeof name == ‘string‘ && typeof callback == ‘function‘){ Obsever[name] ? Obsever[name].push(callback) : Obsever[name] = [callback]; } }
//删除观察者; var remove = function (name, callback){
if(typeof name == ‘string‘){ var list = Obsever[name];
if(list){ if( callback == null){ delete Obsever[name]; }else{ for (var i = 0; i < list.length; i++){ if(callback == list[i]){ list[i] = null; } } } }
}
}
//调用观测者; var broadcase = function (name){ var list = Obsever[name]; if(list){for(var i = 0 ; i < list.length; i++){ var cb = list[i]; if(typeof cb == ‘function‘){ cb.apply(cb,Array.prototype.slice.call(arguments,1)); }else{ list.splice(i,1); i--; } } } } add(‘teacher‘,function(message){alert(message)}); broadcase(‘teacher‘,‘Hi,goodNight‘);
修改了几次;暂时先这样;
标签:style blog io ar color sp for on div
原文地址:http://www.cnblogs.com/clearfix/p/4115801.html