1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>js异步执行</title>
5 </head>
6 <body>
7 <script type="text/javascript">
8 var win = {
9 yes: false,//判断是否直接执行后续fn
10 m: [],//搜集器
11 fn: function (f) { if (this.yes) { f(); } else { this.m.push(f); } return this },
12 fn2: function () {
13 for (var i = 0, j = this.m.length; i < j; i++) {
14 this.m[i]();
15 }
16 this.m.splice(0, this.m.length);//清空数组
17 }
18 }
19 function f1() {
20 var did = win;
21 setTimeout(function () {
22 did.fn2();
23 }, 1000);
24 return did;
25 }
26 function f2() {
27 alert("yes");
28 }
29 function f3() {
30 alert("no");
31 }
32 function f4() {
33 alert("yes and no");
34 }
35 f1().fn(f2).fn(f3).fn(f4);
36 </script>
37 </body>
38 </html>