javascript undo redo

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


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>javascript</title>
</head>

<body>
<input type="button" value=" - " id="a" /> <input type="button" value=" + " id="b" />

<input type="button" value=" - " id="c" /> <input type="button" value=" + " id="d" />
<script type="text/javascript">
/*function a() {
Do([].slice.call(arguments));
}
function Do() {
var temp = (arguments[0]),indicator = arguments.callee;
indicator.stack = [];
indicator.stack[0] = temp;
}
a({c:1,b:2}, 123, function(arg) {
return arg+1;
});
function dir() {
console.log(arguments[0]);
}
dir(Do.stack[0][2](3));*/

function MemoryDo(step){

var indicator,that,fn,slice,args;
(
indicator=arguments.callee,
slice=[].slice
)
if(!(this instanceof indicator)){return new indicator(step);}

indicator.keys={};
that=this;
this.stack=[];
this.index=0;


fn = indicator.prototype;
fn.constructor = indicator;

fn.is=function(o){
return ({}).toString.call(o).slice(8, -1);
}
fn.slice=function(o,index){
return slice.call(o,index || 0);
}
fn.setIndex=function(number){
if(number<0 || this.is(this.index)!=='Number'){return};
this.index=number;
}
fn.getIndex=function(){
return this.is(this.index)==='Number' ? this.index : null;
}
fn.sign=function(){
return ('abcdefghijk'[Math.random().toFixed(1) * 10]+this.randomNumber(10000,10000000));
}
fn.randomNumber=function(a,b){
return Math.round(Math.random()*b+a);
}
fn.dataBuffer=function(){
var temp,socureCall=arguments.callee.caller,flag=arguments[0][arguments[0].length-1],data;
if (socureCall.arguments.length !== arguments[0].length || indicator.keys[flag]==='used') return;

data=this.stack;
if(data.length===this.step){data.shift()};

temp=data[data.length]=[];
temp[temp.length]=(this.slice(arguments[0],0)).concat(this.sn);
temp[temp.length]=arguments.callee.caller;

indicator.keys[this.sn]='used';

this.index=this.stack.length;
}
fn.redo=function(){
//var data=indicator.stack[indicator.stack.length-1];
//console.log(data);
if(this.index===this.stack.length){
this.re=-1;return;
}else{
this.re=1;
this.virtualDo(this.index+=1);
}

}
fn.undo=function(){
//var data=indicator.stack[indicator.stack.length-1];
//console.log(data);

if(this.index===0){
return;
}else{
this.un=1;
this.virtualDo(this.index-=1);
}

}
fn.virtualDo=function(){
var data=this.stack,i=0,len=this.index,temp;
for(;i<len;){
temp=data[i++];
temp[1].apply(undefined,temp[0]);
}
}


this.step=this.is(step)==='Number'? step : 10;
this.sn=this.sign();

}


var a=MemoryDo(4);
var b=MemoryDo(2);
function doIt(o,fn){
a.dataBuffer(arguments);

fn(arguments);
b.dataBuffer(arguments);
}
doIt({a:1,b:2,c:3},function(a){
console.dir(a);
});
doIt({b:2,c:3},function(a){
console.dir(a);
});
doIt({c:3},function(a){
console.dir(a);
});
doIt({d:4},function(a){
console.dir(a);
});
document.getElementById('a').onclick=function(){
a.undo();
}
document.getElementById('b').onclick=function(){
a.redo();
}
document.getElementById('c').onclick=function(){
b.undo();
}
document.getElementById('d').onclick=function(){
b.redo();
}
</script>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值