javascript设计模式第三章之闭包实现命令模式

//html部分


//js部分
var Tv={
  open:function(){
    console.log("open tv");
  },
  close:function(){
    console.log("close tv");
  }
}

var createCommand=function(receiver){
  var execute=function(){
    return receiver.open();//执行命令,打开Tv
  }
  var undo=function(){
    return receiver.close();//执行命令,关闭Tv
  }
  return {
    execute:execute,
    undo:undo
  }
}
var setCommand=function(command){
  document.getElementById("execute").onclick=function(){
    command.execute();//输出打开Tv
  }
  document.getElementById("undo").onclick=function(){
    command.undo();//输出关闭Tv
  }
}
setCommand(createCommand(Tv));

你可能感兴趣的:(javascript设计模式第三章之闭包实现命令模式)