/**
 * observerLIst.js
 *
 * @Author: apple
 * @Date: 2018/4/17 23:00
 */

//通用代码
var observerList = {
    //订阅
    addSubscriber: function addSubscriber(callback) {
        this.subscribers[this.subscribers.length] = callback;
    },
    //退订
    removeSubscriber: function removeSubscriber(callback) {
        for (var i = 0; i < this.subscribers.length; i++) {
            if (this.subscribers[i] === callback) {
                delete this.subscribers[i];
            }
        }
    },
    //发布
    publish: function publish(what) {
        for (var i = 0; i < this.subscribers.length; i++) {
            if (typeof this.subscribers[i] === 'function') {
                this.subscribers[i](what);
            }
        }
    },
    // 将对象o具有观察者功能
    make: function make(o) {
        for (var i in this) {
            o[i] = this[i];
            o.subscribers = [];
        }
    }
};
export default observerList;