免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2062 | 回复: 1
打印 上一主题 下一主题

javascript undo redo [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-09 17:17 |只看该作者 |倒序浏览
javascript undo redo












在浏览器中用调试工具看调用结果


Javascript代码
  1. 1.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
  2. 2.        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  3. 3.<html xmlns="http://www.w3.org/1999/xhtml">   
  4. 4.<head>   
  5. 5.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>   
  6. 6.    <title>javascript</title>   
  7. 7.</head>   
  8. 8.  
  9. 9.<body>   
  10. 10.<input type="button" value=" - " id="a" /> <input type="button" value=" + " id="b" />   
  11. 11.  
  12. 12.<input type="button" value=" - " id="c" /> <input type="button" value=" + " id="d" />   
  13. 13.<script type="text/javascript">   
  14. 14.    /*function a() {  
  15. 15.        Do([].slice.call(arguments));  
  16. 16.    }  
  17. 17.    function Do() {  
  18. 18.        var temp = (arguments[0]),indicator = arguments.callee;  
  19. 19.        indicator.stack = [];  
  20. 20.        indicator.stack[0] = temp;  
  21. 21.    }  
  22. 22.    a({c:1,b:2}, 123, function(arg) {  
  23. 23.        return arg+1;  
  24. 24.    });  
  25. 25.    function dir() {  
  26. 26.        console.log(arguments[0]);  
  27. 27.    }  
  28. 28.    dir(Do.stack[0][2](3));*/  
  29. 29.  
  30. 30.    function MemoryDo(step){   
  31. 31.  
  32. 32.        var indicator,that,fn,slice,args;   
  33. 33.        (   
  34. 34.            indicator=arguments.callee,   
  35. 35.            slice=[].slice   
  36. 36.        )   
  37. 37.        if(!(this instanceof indicator)){return new indicator(step);}   
  38. 38.  
  39. 39.        indicator.keys={};   
  40. 40.        that=this;   
  41. 41.        this.stack=[];   
  42. 42.        this.index=0;   
  43. 43.  
  44. 44.           
  45. 45.        fn = indicator.prototype;   
  46. 46.        fn.constructor = indicator;   
  47. 47.  
  48. 48.        fn.is=function(o){   
  49. 49.            return ({}).toString.call(o).slice(8, -1);   
  50. 50.        }   
  51. 51.        fn.slice=function(o,index){   
  52. 52.            return slice.call(o,index || 0);   
  53. 53.        }   
  54. 54.        fn.setIndex=function(number){   
  55. 55.            if(number<0 || this.is(this.index)!=='Number'){return};   
  56. 56.            this.index=number;   
  57. 57.        }   
  58. 58.        fn.getIndex=function(){   
  59. 59.            return this.is(this.index)==='Number' ? this.index : null;   
  60. 60.        }   
  61. 61.        fn.sign=function(){   
  62. 62.            return ('abcdefghijk'[Math.random().toFixed(1) * 10]+this.randomNumber(10000,10000000));   
  63. 63.        }   
  64. 64.        fn.randomNumber=function(a,b){   
  65. 65.            return Math.round(Math.random()*b+a);   
  66. 66.        }   
  67. 67.        fn.dataBuffer=function(){   
  68. 68.            var temp,socureCall=arguments.callee.caller,flag=arguments[0][arguments[0].length-1],data;   
  69. 69.            if (socureCall.arguments.length !== arguments[0].length || indicator.keys[flag]==='used') return;   
  70. 70.  
  71. 71.            data=this.stack;   
  72. 72.            if(data.length===this.step){data.shift()};   
  73. 73.  
  74. 74.            temp=data[data.length]=[];   
  75. 75.            temp[temp.length]=(this.slice(arguments[0],0)).concat(this.sn);   
  76. 76.            temp[temp.length]=arguments.callee.caller;   
  77. 77.  
  78. 78.            indicator.keys[this.sn]='used';   
  79. 79.  
  80. 80.            this.index=this.stack.length;   
  81. 81.        }   
  82. 82.        fn.redo=function(){   
  83. 83.            //var data=indicator.stack[indicator.stack.length-1];   
  84. 84.            //console.log(data);   
  85. 85.            if(this.index===this.stack.length){   
  86. 86.                this.re=-1;return;   
  87. 87.            }else{   
  88. 88.                this.re=1;   
  89. 89.                this.virtualDo(this.index+=1);   
  90. 90.            }   
  91. 91.  
  92. 92.        }   
  93. 93.        fn.undo=function(){   
  94. 94.            //var data=indicator.stack[indicator.stack.length-1];   
  95. 95.            //console.log(data);   
  96. 96.               
  97. 97.            if(this.index===0){   
  98. 98.                return;   
  99. 99.            }else{   
  100. 100.                this.un=1;   
  101. 101.                this.virtualDo(this.index-=1);   
  102. 102.            }   
  103. 103.  
  104. 104.        }   
  105. 105.        fn.virtualDo=function(){   
  106. 106.            var data=this.stack,i=0,len=this.index,temp;   
  107. 107.            for(;i<len;){   
  108. 108.                temp=data[i++];   
  109. 109.                temp[1].apply(undefined,temp[0]);   
  110. 110.            }   
  111. 111.        }   
  112. 112.  
  113. 113.  
  114. 114.        this.step=this.is(step)==='Number'? step : 10;   
  115. 115.        this.sn=this.sign();   
  116. 116.           
  117. 117.    }   
  118. 118.  
  119. 119.  
  120. 120.var a=MemoryDo(4);   
  121. 121.var b=MemoryDo(2);   
  122. 122.function doIt(o,fn){   
  123. 123.    a.dataBuffer(arguments);   
  124. 124.  
  125. 125.    fn(arguments);   
  126. 126.    b.dataBuffer(arguments);   
  127. 127.}   
  128. 128.doIt({a:1,b:2,c:3},function(a){   
  129. 129.    console.dir(a);   
  130. 130.});   
  131. 131.doIt({b:2,c:3},function(a){   
  132. 132.    console.dir(a);   
  133. 133.});   
  134. 134.doIt({c:3},function(a){   
  135. 135.    console.dir(a);   
  136. 136.});   
  137. 137.doIt({d:4},function(a){   
  138. 138.    console.dir(a);   
  139. 139.});   
  140. 140.document.getElementById('a').onclick=function(){   
  141. 141.    a.undo();   
  142. 142.}   
  143. 143.document.getElementById('b').onclick=function(){   
  144. 144.    a.redo();   
  145. 145.}   
  146. 146.document.getElementById('c').onclick=function(){   
  147. 147.    b.undo();   
  148. 148.}   
  149. 149.document.getElementById('d').onclick=function(){   
  150. 150.    b.redo();   
  151. 151.}   
  152. 152.</script>   
  153. 153.</body>   
  154. 154.</html>
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-24 20:24 |只看该作者
谢谢分享  希望于楼主多多交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP