-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript_designMode.html
109 lines (106 loc) · 2.43 KB
/
javascript_designMode.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js设计模式</title>
</head>
<body>
<script>
let scope = (function () {
let events = {};
return {
on (name, handler) {
let index;
if (events[name]) {
index = events[name].push(handler) - 1;
} else {
events[name] = [handler];
index = 0
}
},
off (name) {
if (! events[name]) return;
delete events[name];
},
emit (name, msg) {
if (! events[name]) {
return;
}
events[name].forEach((v, i) => {
v(msg)
})
}
}
});
let obj ={
events : {},
on (type, fn) {
if (typeof fn !=='function') return;
if (!this.events[type]) {
this.events[type] = [fn];
} else {
this.events[type].push(fn);
}
},
emit (type, msg) {
if (!this.events[type]) {
return;
}
this.events[type].forEach((val, index) => {
val(msg);
})
}
};
obj.on('click', first);
obj.on('click', second);
obj.on('click', third);
function doingSomething () {
obj.emit('click', 'hello domingo')
}
doingSomething();
function first (msg) {
console.log(msg)
}
function second () {
console.log('domingo')
}
function third () {
console.log('welcome to my live')
}
// function selfOn (ele, type, fn) {
// if (!ele[type]) {
// ele[type] = []
// }
// let a = ele[type];
// for (let i = 0; i < a.length; i++) {
// if (a[i] === fn) return;
// }
// a.push(fn);
// }
// function selfOff (type,fn) {
//
// }
// function fire(type, e) {
// let a = this[type];
// if (a) {
// for (let i =0; i < a.length; i++ ) {
// a[i].call(this, e);
// }
// }
// }
// function test () {
// console.log('11111111')
// }
// selfOn()
// function down () {
//
// }
// function move () {
//
// }
// function up () {
//
// }
</script>
</body>
</html>