最近应用前端操作栈记录操作 event 事件; 栈是一种 LIFO(Last-In-First-Out)的数据结构,即最先增加到容器中的我的项目最早被移出。这种数据结构能够限度插入和删除我的项目。而栈中项的插入和移出只会产生在栈的顶部。stack[]提供 push()
和pop()
办法,用来实现相似栈的行为。
1、先上实例
function Stack() {this.stack = [];
this.actionIndex = 0;
}
Stack.prototype.exec = function (action) {action.exec();
this.stack[this.actionIndex++] = action;
this.stack.length = this.actionIndex;
return this;
};
Stack.prototype.redo = function () {if (this.canRedo()) {this.stack[this.actionIndex++].exec();}
return this;
};
Stack.prototype.undo = function () {if (this.canUndo()) {this.stack[--this.actionIndex].undo();}
};
Stack.prototype.canRedo = function(){return this.actionIndex < this.stack.length;};
Stack.prototype.canUndo = function(){return this.actionIndex > 0;};
function BaseAction(target, newValue,opts){
this.target = target;
this.newValue = newValue;
this.oldValue = this._get();}
BaseAction.extend = function(setter,getter){function Action(){BaseAction.apply(this,arguments);
}
Action.prototype = BaseAction.prototype;
Action.prototype.constructor = Action;
Action.prototype._set = setter;
if(getter){Action.prototype._get = getter;}
return Action;
};
2、应用事件办法将两个字符串推入到数组的开端,并将返回的后果保留在变量 index 中。用 index 记录栈的存取地位,