function Command() {
this.execute = function() {
// 执行具体操作
};
}
function Invoker() {
this.command = null;
this.setCommand = function(command) {
this.command = command;
};
this.executeCommand = function() {
this.command.execute();
};
}
var command = new Command(); var invoker = new Invoker(); invoker.setCommand(command); invoker.executeCommand();
function Command(param1, param2) {
this.execute = function() {
// 使用参数执行具体操作
};
}
function Invoker() {
this.command = null;
this.setCommand = function(command) {
this.command = command;
};
this.executeCommand = function(param1, param2) {
this.command.execute(param1, param2);
};
}
// HTML
// JavaScript
var command = {
execute: function() {
alert('Hello, Command Pattern!');
}
};
var invoker = {
button: document.getElementById('btn'),
setCommand: function(command) {
this.button.addEventListener('click', function() {
command.execute();
});
}
};
invoker.setCommand(command);
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com
