您的位置:首页 > Web前端 > JavaScript

js实现观察者模式

2016-06-20 12:50 465 查看
var publisher = {
subscribers: {
any: [] // event type: subscribers
},

subscribe: function (fn, type) {
type = type || 'any';
if (typeof this.subscribers[type] === "undefined") {
this.subscribers[type] = [];
}
this.subscribers[type].push(fn);
},

unsubscribe: function (fn, type) {
this.visitSubscribers('unsubscribe', fn, type);
},

publish: function (publication, type) {
this.visitSubscribers('publish', publication, type);
},

visitSubscribers: function (action, arg, type) {
var pubtype = type || 'any',
subscribers = this.subscribers[pubtype],
i,
max = subscribers.length;

for (i = 0; i < max; i += 1) {
if (action === 'publish') {
subscribers[i](arg);
} else {
if (subscribers[i] === arg) {
subscribers.splice(i, 1);
}
}
}

}
};

function makePublisher(o) {
var i;
for (i in publisher) {
if (publisher.hasOwnProperty(i) && typeof publisher[i] === "function") {
o[i] = publisher[i];
}
}

o.subscribers = {any: []};
}

var paper = {
daily: function () {
this.publish("big news today");
},

monthly: function () {
this.publish("interesting analysis", "monthly");
}
};

makePublisher(paper);

var joe = {
drinkCoffee: function (paper) {
console.log('Just read ' + paper);
},

sundayPreNap: function (monthly) {
console.log('About to fall asleep reading this ' + monthly);
}
};

paper.subscribe(joe.drinkCoffee);
paper.subscribe(joe.sundayPreNap, 'monthly');

paper.daily();
paper.daily();
paper.daily();
paper.monthly();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: