Skip to content

Commit 76f0305

Browse files
authored
Update common.js
1 parent 35176c3 commit 76f0305

File tree

1 file changed

+80
-15
lines changed

1 file changed

+80
-15
lines changed
Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,96 @@
1-
var salesOffices = {}; // 定义售楼处
2-
salesOffices.clientList = []; // 缓存列表,存放订阅者的回调函数
1+
/***************ES6 common*****************/
2+
class Event {
3+
constructor() { }
4+
// 事件容器
5+
handlers = {}
6+
7+
// 事件添加方法,参数有事件名和事件方法
8+
on(type, handler) {
9+
console.log('绑定事件', type, handler)
10+
// 首先判断handlers内有没有type事件容器,没有则创建一个新数组容器
11+
if (!(type in this.handlers)) {
12+
this.handlers[type] = []
13+
}
14+
// 将事件存入
15+
this.handlers[type].push(handler)
16+
}
17+
18+
// 触发事件两个参数(事件名,参数)
19+
emit(type, ...params) {
20+
console.log('触发事件', type, ...params)
21+
// 若没有注册该事件则抛出错误
22+
if (!(type in this.handlers)) {
23+
return new Error('未注册该事件')
24+
}
25+
// 遍历触发
26+
this.handlers[type].forEach(handler => {
27+
handler(...params)
28+
})
29+
}
30+
31+
// 事件移除参数(事件名,删除的事件,若无第二个参数则删除该事件的订阅和发布)
32+
off(type, handler) {
33+
console.log('触发事件移除', type, handler)
34+
// 无效事件抛出
35+
if (!(type in this.handlers)) {
36+
return new Error('无效事件')
37+
}
38+
//清除所有事件
39+
if (!type) {
40+
this.handlers = {}
41+
}
42+
// 移除指定类型下的所有事件
43+
else if (!handler) {
44+
delete this.handlers[type]
45+
} else {
46+
if (typeof handler !== "function") {
47+
return new Error('无效handler')
48+
}
49+
const idx = this.handlers[type].findIndex(ele => ele === handler)
50+
// 抛出异常事件
51+
if (idx === undefined) {
52+
return new Error('无该绑定事件')
53+
}
54+
// 移除事件
55+
this.handlers[type].splice(idx, 1)
56+
if (this.handlers[type].length === 0) {
57+
delete this.handlers[type]
58+
}
59+
}
60+
}
61+
}
62+
63+
64+
65+
/********************ES5 demo***********************/
66+
var salesOffices = {}; // 定义售楼处
67+
salesOffices.clientList = []; // 缓存列表,存放订阅者的回调函数
368

469
salesOffices.listen = function( key, fn ){
5-
if ( !this.clientList[ key ] ){ // 如果还没有订阅过此类消息,给该类消息创建一个缓存列表
70+
if ( !this.clientList[ key ] ){ // 如果还没有订阅过此类消息,给该类消息创建一个缓存列表
671
this.clientList[ key ] = [];
772
}
8-
this.clientList[ key ].push( fn ); // 订阅的消息添加进消息缓存列表
73+
this.clientList[ key ].push( fn ); // 订阅的消息添加进消息缓存列表
974
};
1075

11-
salesOffices.trigger = function(){ // 发布消息
12-
var key = Array.prototype.shift.call( arguments ), // 取出消息类型
13-
fns = this.clientList[ key ]; // 取出该消息对应的回调函数集合
14-
if ( !fns || fns.length === 0 ){ // 如果没有订阅该消息,则返回
76+
salesOffices.trigger = function(){ // 发布消息
77+
var key = Array.prototype.shift.call( arguments ), // 取出消息类型
78+
fns = this.clientList[ key ]; // 取出该消息对应的回调函数集合
79+
if ( !fns || fns.length === 0 ){ // 如果没有订阅该消息,则返回
1580
return false;
1681
}
1782
for( var i = 0, fn; fn = fns[ i++ ]; ){
18-
fn.apply( this, arguments ); // (2) // arguments 是发布消息时附送的参数
83+
fn.apply( this, arguments ); // (2) // arguments 是发布消息时附送的参数
1984
}
2085
};
2186

22-
salesOffices.listen( 'squareMeter88', function( price ){ // 小明订阅88 平方米房子的消息
23-
console.log( '价格= ' + price ); // 输出: 2000000
87+
salesOffices.listen( 'squareMeter88', function( price ){ // 小明订阅88 平方米房子的消息
88+
console.log( '价格= ' + price ); // 输出: 2000000
2489
});
2590

26-
salesOffices.listen( 'squareMeter110', function( price ){ // 小红订阅110 平方米房子的消息
27-
console.log( '价格= ' + price ); // 输出: 3000000
91+
salesOffices.listen( 'squareMeter110', function( price ){ // 小红订阅110 平方米房子的消息
92+
console.log( '价格= ' + price ); // 输出: 3000000
2893
});
2994

30-
salesOffices.trigger( 'squareMeter88', 2000000 ); // 发布88 平方米房子的价格
31-
salesOffices.trigger( 'squareMeter110', 3000000 ); // 发布110 平方米房子的价格
95+
salesOffices.trigger( 'squareMeter88', 2000000 ); // 发布88 平方米房子的价格
96+
salesOffices.trigger( 'squareMeter110', 3000000 ); // 发布110 平方米房子的价格

0 commit comments

Comments
 (0)