最近使用前端操作栈记录操作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记录栈的存取位置,
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。