返回

命令模式

var setCommand = function({
element = null,
eventType = "onclick",
command = null,
commandType = "execute"
}) {
if (element && eventType && typeof element === "object" && eventType in element) {
    if (command && commandType && typeof command === "object" && commandType in command) {
    element[eventType] = command[commandType];
    }
}
};

var command = (function() {
let _i = 0;
return {
    execute: function() {
    _i++;
    console.log(_i);
    },
    undo: function() {
    _i--;
    consoloe.log(_i);
    }
};
})();
var ExecuteBtnbtn  = document.querySelector('#ExecuteBtnbtn');
var UndoBtn = document.querySelector('#UndoBtn');
setCommand({
    element: ExecuteBtnbtn,
    command:command
})
setCommand({
    element: UndoBtn,
    command:command,
    commandType: 'undo'
})