SOURCE

console 命令行工具 X clear

                    
>
console
class SalesOffice {
    constructor() {
        this.clientList = [];
    }

    subscribe(evt, fn) {
        let targets = this.getTargets(evt);
        targets.push(fn);
        return fn;
    }

    unsubscribe(evt, fn) {
        let targets = this.getTargets(evt);
        targets.splice(targets.indexOf(fn), 1);
    }

    trigger(evt, ...args) {
        this.getTargets(evt).forEach(fn => fn.apply(this, args));
    }

    getTargets(evt) {
        if (!(this.clientList[evt] instanceof Array)) {
            this.clientList[evt] = [];
        }
        return this.clientList[evt];
    }


}

class Buyer {
    constructor({ name, type }) {
        this.name = name;
        this.type = type;
        this.listenSalesInfo();
    }

    listenSalesInfo() {
        let name = this.name, type = this.type;
        this.listener = BGY.subscribe(type, function (price, size) {
            console.log(name, type);
            console.log('价格', price);
            console.log('面积', size)
        });
    }

    doneBuying() {
        console.log(this.name, 'doneBuying');
        BGY.unsubscribe(this.type, this.listener);
    }
}


const BGY = new SalesOffice();
const HD = new SalesOffice();


const LY = new Buyer({
    name: 'LY',
    type: 'TypeA'
});

const SY = new Buyer({
    name: 'SY',
    type: 'TypeB'
});

const YQ = new Buyer({
    name: 'YQ',
    type: 'TypeB'
});



BGY.trigger('TypeA', 100, '300m2')
BGY.trigger('TypeB', 300, '400m2')
LY.doneBuying()
YQ.doneBuying()

BGY.trigger('TypeA', 100, '300m2')
BGY.trigger('TypeB', 100, '300m2')
  
<pre>
    
</pre>