# 发布订阅

class EventBus {
  constructor() {
    this.task = {};
  }

  on = function (key, cb) {
    if (Array.isArray(this.task[key])) {
      this.task[key].push(cb);
    } else {
      this.task[key] = [cb];
    }
  };
  emit = function () {
    var key = arguments[0];
    var args = Array.prototype.slice.call(arguments, 1);
    var cbArr = this.task[key];
    if (Array.isArray(cbArr) && cbArr.length > 0) {
      cbArr.forEach((cb) => {
        cb(args);
      });
    }
  };

  removeOn = function (key, cb) {
    var cbArr = this.task[key];
    if (!Array.isArray(cbArr)) return;

    for (let i = 0; i < cbArr.length; i++) {
      if (cbArr[i] === cb) {
        cbArr.splice(i, 1);
        break;
      }
    }
  };
}

const message = new EventBus();

function fn1() {
  console.log('fn1', [...arguments]);
}

function fn2() {
  console.log('fn2', [...arguments]);
}

message.on('messageClick', fn1);

message.on('messageClick', fn2);

message.emit('messageClick', 1, 2, 3, 4);

message.removeOn('messageClick', fn1);

message.emit('messageClick', 1, 2, 3, 4);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Last Updated: 6/27/2023, 7:40:45 PM