码迷,mamicode.com
首页 > 其他好文 > 详细

发布-订阅模式

时间:2016-04-05 19:45:02      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>

 


</head>
<body>
<script type="text/javascript">

 

var event={
clientList:[],
listen:function(key,fn){

if(!this.clientList[key]){
this.clientList[key]=[];

}
this.clientList[key].push(fn);

},
trigger:function(){
var key=Array.prototype.shift.call(arguments);

var fns=this.clientList[key];

if(!fns||fns.length===0) {

return false;

}
for(var i=0,fn;fn=fns[i++];){
fn.apply(this,arguments);

}

},
remove:function(key,fn){
var fns=this.clientList[key];
if(!fns){

return false;
}
if(!fn){

fns&&(fns.length=0);
}
else{

for(var i=fns.length-1;i>=0;i--){
var _fn=fns[i];
if(_fn===fn){

fns.splice(i,1);

}

}

}


}

}


event.listen("aa",a=function(price){alert(price)});
event.listen("aa",b=function(price){alert(price)});
event.remove("aa",a);
event.trigger("aa","111");
//event.trigger("aa","111");

var nav=(function(){

var a=1;
return bniu:function(){


alert(a+3);
}

 

})();
nav.bniu();

</script>

</body>
</html>

发布-订阅模式

标签:

原文地址:http://www.cnblogs.com/yaopj/p/5356276.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!