马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
使多个对象都有机会处理请求,从而避免了请求的发送者与多个接收者直接的耦合关系,将这些接收者毗连成一条链,顺着这条链通报该请求,直到找到能处理该请求的对象。
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <input type="text" id="input">
- <button id="button">注册</button>
- <script>
- function checkEmpty() {
- if (input.value.length === 0) {
- alert('不能为空');
- return false;
- } else {
- return true;
- }
- }
- function checkLength() {
- if (input.value.length < 6) {
- alert('长度不能小于6');
- return false;
- } else {
- return true;
- }
- }
- class Chain{
- constructor(fn) {
- this.fn = fn;
- this.next = null;
- }
- setNext(next) {
- this.next = next;
- }
- run() {
- let result = this.fn.apply(this, arguments);
- if (result === true && this.next !== null) {
- return this.next.run.apply(this.next, arguments);
- }
- return result;
- }
- }
- let chain1 = new Chain(checkEmpty);
- let chain2 = new Chain(checkLength);
- chain1.setNext(chain2);
- button.onclick = function () {
- chain1.run();
- }
- </script>
- </body>
- </html>
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |