<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>宏命令</title> </head> <body> </body> <script type="text/javascript"> /* 宏命令是一组命令的集合,通过执行宏命令的方式,可以一次执行一批命令。想象一下,家 里有一个万能遥控器,每天回家的时候,只要按一个特别的按钮,它就会帮我们关上房间门,顺 便打开电脑并登录 QQ。 */ var closeDoorCommand = { excute: function () { console.log('关门'); } }; var openPcCommand = { excute: function () { console.log('开电脑'); } }; var openQQCommand = { excute: function () { console.log('登录QQ'); } }; var MacroCommand = function () { return { commandsList: [], add: function (command) { this.commandsList.push(command); }, excute: function () { for (var i=0, command; command = this.commandsList[i++];) { command.excute(); } } } }; var macroCommand = MacroCommand(); macroCommand.add(closeDoorCommand); macroCommand.add(openPcCommand); macroCommand.add(openQQCommand); macroCommand.excute(); </script> </html>