forked from alexwjj/alexwjj.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (121 loc) · 2.98 KB
/
index.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function myCall(ctx = window, ...args) {
ctx = ctx || window;
// 为context 创建一个 Symbol(保证不会重名)属性,将当前函数赋值给这个属性
const fn = Symbol();
ctx[fn] = this;
// 处理参数,传入第一个参数后的其余参数
const res = ctx[fn](...args);
// 调用函数后即删除该Symbol属性
delete ctx[fn];
return res;
}
function myApply(ctx = window, args) {
ctx = ctx || window;
const fn = Symbol();
ctx[fn] = this;
let result = null;
if (Array.isArray(args)) {
result = ctx[fn](...args);
} else {
result = ctx[fn]();
}
delete ctx[fn];
return res;
}
function myBind(ctx, ...args1) {
const _this = this;
return function F(...args2) {
if (this instanceof F) {
// 判断是否为构造函数调用,如果是则使用new调用当前函数
return new _this(...args1, ...args2)
} else {
// 如果不是,使用apply,将context和处理好的参数传入
return _this.apply(ctx, args1.concat(args2))
}
}
}
class PubSub{
constructor() {
this.subscribers = [];
}
subscribe (topic, callback) {
let callbacks = this.subscribers[topic]
if(!callbacks) {
this.subscribers[topic] = [callback]
} else {
callbacks.push(callback)
}
}
publish(topic, ...args) {
let callbacks = this.subscribers[topic] || [];
callbacks.forEach(callback => callback(...args))
}
}
function aEvent(num) {
console.log('aEvent:'+num);
}
function bEvent(num) {
console.log('bEvent:'+num);
}
let pubSub = new PubSub();
pubSub.subscribe('aaa', aEvent)
pubSub.subscribe('bbb', bEvent)
pubSub.publish('aaa', '11111111')
pubSub.publish('bbb', '11111111')
// 观察者模式
class Subject {
constructor() {
this.observers = [];
}
add(observer) {
this.observers.push(observer)
}
notify(...args) {
this.observers.forEach(observer => observer.update(...args))
}
}
class Observer {
update(...args) {
console.log(...args);
}
}
// 创建观察者ob1
let ob1 = new Observer();
// 创建观察者ob2
let ob2 = new Observer();
// 创建目标sub
let sub = new Subject();
// 目标sub添加观察者ob1 (目标和观察者建立了依赖关系)
sub.add(ob1);
// 目标sub添加观察者ob2
sub.add(ob2);
// 目标sub触发SMS事件(目标主动通知观察者)
sub.notify('I fired `SMS` event');
class Subject {
constructor() {
this.observers = [];
}
add(observer) {
this.observers.push(observer);
}
notify(...args) {
this.observers.forEach(observer => observer.update(...args));
}
}
class Observer {
update(...args) {
console.log(...args);
}
}
// 创建观察者ob1
let ob1 = new Observer();
// 创建观察者ob2
let ob2 = new Observer();
// 创建目标sub
let sub = new Subject();
// 目标sub添加观察者ob1 (目标和观察者建立了依赖关系)
sub.add(ob1);
// 目标sub添加观察者ob2
sub.add(ob2);
// 目标sub触发SMS事件(目标主动通知观察者)
sub.notify('I fired `SMS` event');