console
let _subscribe = function () {
class Sub {
constructor() {
this.$pond = [];
}
add(func) {
let flag = this.$pond.some(item => {
return func === item;
})
!flag ? this.$pond.push(func) : null;
}
remove(func) {
let $pond = this.$pond;
for (let i = 0; i < $pond.length; i++) {
if ($pond[i] === func) {
this.$pond[i] === null;
break;
}
}
}
fire(...args) {
let $pond = this.$pond;
for (let i = 0; i < $pond.length; i++) {
if ($pond[i] === null) {
this.$pond.splice(i, 1)
continue;
}
$pond[i].call(this, ...args);
}
}
}
return function subscribe() {
return new Sub();
}();
}
let pond = _subscribe();
document.getElementById('submit').onclick = function (ev) {
console.log(pond);
pond.fire(ev);
}
let fn1 = function () {
console.log(1);
}
let fn2 = function () {
console.log(2);
pond.remove(fn1);
}
let fn3 = function () {
console.log(3);
}
let fn4 = function (ev) {
console.log(4,ev);
}
pond.add(fn1);
pond.add(fn2);
pond.add(fn3);
pond.add(fn4);
<div>
<button id='submit'>excute</button>
</div>