美文网首页
js 宏命令模式

js 宏命令模式

作者: _嘿嘿_ | 来源:发表于2018-10-09 16:12 被阅读0次

var closeDoorCommand = {
execute: function(){
console.log( '关门' );
}
};
var openPcCommand = {
execute: function(){
console.log( '开电脑' );
}
};
var openQQCommand = {
execute: function(){
console.log( '登录 QQ' );
}
};
var MacroCommand = function(){
return {
commandsList: [],
add: function( command ){
this.commandsList.push( command );
},
execute: function(){
for ( var i = 0, command; command = this.commandsList[ i++ ]; ){
command.execute();
}
}
}
};
var macroCommand = MacroCommand();
macroCommand.add( closeDoorCommand );
macroCommand.add( openPcCommand );
macroCommand.add( openQQCommand );
macroCommand.execute();

相关文章

网友评论

      本文标题:js 宏命令模式

      本文链接:https://www.haomeiwen.com/subject/qmqyaftx.html