Skip to content

Commit 0859a2a

Browse files
committed
分离目录
1 parent 4e30594 commit 0859a2a

File tree

4 files changed

+255
-8
lines changed

4 files changed

+255
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5+
<script src="http://img.mukewang.com/down/540812440001e40e00000000.js" type="text/javascript"></script>
6+
<script src="http://img.mukewang.com/down/541f6ff70001a0a500000000.js" type="text/javascript"></script>
7+
<title></title>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
14+
function Callbacks(options) {
15+
var list = [];
16+
var self;
17+
var firingStart;
18+
var memory;
19+
20+
function _fire(data) {
21+
memory = options === 'memory' && data;
22+
firingIndex = firingStart || 0;
23+
firingStart = 0;
24+
firingLength = list.length;
25+
for (; list && firingIndex < firingLength; firingIndex++) {
26+
list[firingIndex](data)
27+
}
28+
}
29+
30+
self = {
31+
add: function(fn) {
32+
var start = list.length;
33+
list.push(fn)
34+
if (memory) {
35+
firingStart = start; //获取最后一值
36+
_fire(memory);
37+
}
38+
},
39+
fire: function(args) {
40+
if (list) {
41+
_fire(args)
42+
}
43+
}
44+
}
45+
return self;
46+
}
47+
48+
49+
function fn1(val) {
50+
show('fn1 says ' + val);
51+
}
52+
53+
function fn2(val) {
54+
show('fn2 says ' + val);
55+
}
56+
57+
function fn3(val) {
58+
show('fn3 says ' + val);
59+
}
60+
61+
var cbs = Callbacks('memory');
62+
cbs.add(fn1);
63+
cbs.fire('foo');
64+
65+
66+
67+
cbs.add(fn2);
68+
cbs.fire('bar');
69+
70+
71+
cbs.add(fn3);
72+
cbs.fire('aaron')
73+
74+
75+
76+
</script>
77+
78+
</body>
79+
</html>

version/慕课网/回调函数/once的设计.html

+13-8
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,40 @@
1212
<script type="text/javascript">
1313

1414

15-
function Callbacks() {
15+
function Callbacks(options) {
1616
var list = [];
1717
var self;
1818
self = {
1919
add: function(fn) {
2020
list.push(fn)
2121
},
2222
fire: function(args) {
23-
list.forEach(function(fn) {
24-
fn(args);
25-
})
23+
if(list){
24+
list.forEach(function(fn) {
25+
fn(args);
26+
})
27+
if(options === 'once'){
28+
list = undefined;
29+
}
30+
}
2631
}
2732
}
2833
return self;
2934
}
3035

36+
3137
function fn1(val) {
3238
show('fn1 says:' + val);
3339
}
3440
function fn2(val) {
3541
show('fn2 says ' + val);
3642
}
3743

38-
var cbs = Callbacks();
44+
45+
var cbs = Callbacks('once');
3946
cbs.add(fn1);
4047
cbs.fire('foo');
41-
cbs.add(fn2);
42-
cbs.fire('bar')
43-
48+
cbs.fire('foo');
4449

4550

4651
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5+
<script src="http://img.mukewang.com/down/540812440001e40e00000000.js" type="text/javascript"></script>
6+
<script src="http://img.mukewang.com/down/541f6ff70001a0a500000000.js" type="text/javascript"></script>
7+
<title></title>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
function Callbacks(options) {
14+
var list = [];
15+
var self;
16+
var firingStart;
17+
var memory;
18+
19+
function _fire(data) {
20+
memory = options === 'memory' && data;
21+
firingIndex =
22+
firingStart || 0;
23+
firingStart = 0;
24+
firingLength = list.length;
25+
for (; list && firingIndex < firingLength; firingIndex++) {
26+
if (list[firingIndex](data) === false && options === 'stopOnFalse') {
27+
break;
28+
}
29+
}
30+
}
31+
32+
self = {
33+
add: function(fn) {
34+
var start = list.length;
35+
if (options == 'unique') {
36+
if (-1 === list.indexOf(fn)) {
37+
list.push(fn)
38+
}
39+
} else {
40+
list.push(fn)
41+
}
42+
if (memory) {
43+
firingStart = start; //获取最后一值
44+
_fire(memory);
45+
}
46+
},
47+
fire: function(args) {
48+
if (list) {
49+
_fire(args)
50+
}
51+
}
52+
}
53+
return self;
54+
}
55+
56+
57+
function fn1( value ){
58+
show( value );
59+
return false;
60+
}
61+
62+
function fn2( value ){
63+
fn1( "fn2 says: " + value );
64+
return false;
65+
}
66+
67+
var callbacks = Callbacks("stopOnFalse");
68+
callbacks.add(fn1);
69+
callbacks.fire("foo");
70+
71+
callbacks.add(fn2);
72+
73+
74+
75+
76+
</script>
77+
78+
</body>
79+
</html>
80+
81+
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5+
<script src="http://img.mukewang.com/down/540812440001e40e00000000.js" type="text/javascript"></script>
6+
<script src="http://img.mukewang.com/down/541f6ff70001a0a500000000.js" type="text/javascript"></script>
7+
<title></title>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
14+
function Callbacks(options) {
15+
var list = [];
16+
var self;
17+
var firingStart;
18+
var memory;
19+
20+
function _fire(data) {
21+
memory = options === 'memory' && data;
22+
firingIndex = firingStart || 0;
23+
firingStart = 0;
24+
firingLength = list.length;
25+
for (; list && firingIndex < firingLength; firingIndex++) {
26+
list[firingIndex](data)
27+
}
28+
}
29+
30+
self = {
31+
add: function(fn) {
32+
var start = list.length;
33+
if (options == 'unique') {
34+
if (-1 === list.indexOf(fn)) {
35+
list.push(fn)
36+
}
37+
} else {
38+
list.push(fn)
39+
}
40+
if (memory) {
41+
firingStart = start; //获取最后一值
42+
_fire(memory);
43+
}
44+
},
45+
fire: function(args) {
46+
if (list) {
47+
_fire(args)
48+
}
49+
}
50+
}
51+
return self;
52+
}
53+
54+
55+
function fn1(val) {
56+
show('fn1 says ' + val);
57+
}
58+
var callbacks = Callbacks( "unique" );
59+
callbacks.add( fn1 );
60+
callbacks.add( fn1 ); // 重复添加
61+
callbacks.add( fn1 );
62+
callbacks.fire( "foo" );
63+
64+
65+
66+
</script>
67+
68+
</body>
69+
</html>

0 commit comments

Comments
 (0)