阅读背景:

前端学习心得-javascript设计模式与开发实践-命令封装模式_OJCC的博客

来源:互联网 

命令封装模式主要用于分离请求的发起者与接受者

1、把请求封装为对象

var TV = {
name: "tv",
open: function(){
SwitchObj("#0f0");
console.log("打开")
},
close: function(){
SwitchObj("#f00");
console.log("关闭")
}
}
var air_conditioner = {//shen
name: "air_conditioner",
open: function(){
SwitchObj("#0f0");
console.log("air_conditioner 打开")
},
close: function(){
SwitchObj("#f00");
console.log("air_conditioner 关闭")
}
}
var openCommand = function(receiver){//指定接收方
console.log("execute receiver:"+receiver.name);
this.receiver = receiver;
}
openCommand.prototype.execute = function(){
this.receiver.open();
}
openCommand.prototype.undo = function(){
this.receiver.close();
}var TV



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: