-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
90 lines (74 loc) · 1.96 KB
/
Copy pathindex.html
File metadata and controls
90 lines (74 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!--
* @Author: junchao
* @Date: 2021-01-07 13:42:08
* @LastEditTime: 2021-01-07 13:51:11
* @LastEditors: junchao
* @Description:
* @FilePath: /JavaScriptDesignPatterns/command/index.html
* @可以输入预定的版权声明、个性签名、空行等
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>命令模式例子——菜单命令</title>
</head>
<body>
<button class="ref">刷新菜单</button>
<button class="add">添加子菜单</button>
<button class="del">删除子菜单</button>
</body>
<script>
//菜单对象
var Menu = {
refresh: function () {
console.log("刷新菜单");
}
};
//子菜单
var SubMenu = {
add: function () {
console.log('增加子菜单');
},
del: function () {
console.log('删除子菜单');
}
};
//封装刷新菜单命令
var refreshMenuCommand = function (receiver) {
this.receiver = receiver;
}
refreshMenuCommand.prototype.execute = function () {
this.receiver.refresh();
}
//封装添加子菜单命令
var addSubMenuCommand = function (receiver) {
this.receiver = receiver;
}
addSubMenuCommand.prototype.execute = function () {
this.receiver.add();
}
//封装删除子菜单命令
var delSubMenuCommand = function (receiver) {
this.receiver = receiver;
}
delSubMenuCommand.prototype.execute = function () {
this.receiver.del();
}
//设置命令函数
function setCommand(btn, command) {
btn.addEventListener("click", function () {
command.execute();
})
};
//client客户调用
var refreshCommand = new refreshMenuCommand(Menu);
var addCommand = new addSubMenuCommand(SubMenu);
var delCommand = new delSubMenuCommand(SubMenu);
var btn = document.querySelectorAll("button");
setCommand(btn[0], refreshCommand);
setCommand(btn[1], addCommand);
setCommand(btn[2], delCommand);
</script>
</html>