Recently, the front-end operation stack was used to record operations event event ; Is a kind of the stack LIFO(Last-In-First-Out) Data structure of , That is, the items that are first added to the container are first removed . This data structure can limit the insertion and deletion of items . The insertion and removal of items in the stack only occur at the top of the stack .stack[] Provide push()
and pop()
Method , Used to implement stack like behavior .
1、 Let's start with an example
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、 Use the event method to push two strings to the end of the array , And save the returned result in the variable index in . use index Record the access location of the stack ,