码迷,mamicode.com
首页 > 编程语言 > 详细

javascript设计模式——Observer

时间:2015-03-06 16:49:47      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

1.基本的Observer模式

var Observer = function(){
    this.list = [];
}
Observer.prototype.sub = function(func){
    this.list.push(func);
}
Observer.prototype.pub = function(msg){
    for(var i = 0; i<this.list.length; i ++){
        this.list[i](msg)
    }
}
Observer.prototype.unsub = function(func){
    var index = this.list.indexOf(func);
    this.list.splice(index,1);
}
var ob = new Observer();
function func(msg){
    console.log(msg)
}
ob.sub(func);
ob.pub("Hello");
ob.unsub(func);
ob.pub("useless");

 

javascript设计模式——Observer

标签:

原文地址:http://www.cnblogs.com/winderby/p/4318581.html

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