var opendoor = { execute: function () { console.log("开门"); } }; var closedoor = { execute: function () { console.log("关门"); } }; var lighton = { execute: function () { console.log("开灯"); } }; var cmd = function () { return { cmdList: [], add:function(c){ this.cmdList.push(c); }, execute: function () { for (var i = 0, c; c = this.cmdList[i++];) { c.execute(); } } } }; var command = cmd(); command.add(opendoor); command.add(closedoor); command.add(lighton); command.execute();
通过执行宏命令方式,可以一次执行一批命令。