JavaScript 中不經常使用
對應不到經典場景javascript
//'object.creat'用到了原型模式的思想(雖然不是java中的clone) //基於一個原型建立一個對象 var prototype = { gatName: function() { return this.first + " " + this.last; }, say: function() { console.log("hello"); } }; // 基於原型建立x var x = Object.create(prototype); x.first = "A"; x.last = "B"; console.log(x.gatName()); x.say(); //基於原型建立y var y = Object.create(prototype); y.first = "A"; y.last = "B"; console.log(y.gatName()); y.say();
class ColorShap { yellowCircle() { console.log("yellow circle"); } redCircle() { console.log("red circle"); } yellowTriangle() { console.log("yellow triangle"); } redTriangle() { console.log("red triangle"); } } // 測試 let cs = new ColorShap(); cs.yellowCircle(); cs.redCircle(); cs.yellowTriangle(); cs.redTriangle;
上面代碼改進後html
class Color { constructor(name) { this.name = name; } } class Shap { constructor(name, color) { this.name = name; this.color = color; } draw() { console.log(`${this.color.name} ${this.name}`); } } // 測試代碼 let red = new Color("red"); let yellow = new Color("yellow"); let circle = new Shap("circle", red); circle.draw(); let triabgle = new Shap("triangle", yellow); triabgle.draw();
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> </head> <body> <div id="div1" class="container"> <p>123</p> <p>456</p> </div> </body> </html> <script> var 組合模式 = { tag: "div", attr: { id: "div1", className: "container" }, children: [ { tag: "p", attr: {}, children: ["123"] }, { tag: "p", attr: {}, children: ["456"] } ] }; </script>
總體和單個節點的數據結構也一致java
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> </head> <body> <!-- 無限下拉列表,將事件代理到高層節點上 --> <!-- 若是都綁定到`<a>`標籤,對內存開銷太大 --> <div id="div1"> <a href="#">a1</a> <a href="#">a2</a> <a href="#">a3</a> <a href="#">a4</a> <!-- 無限下拉列表 --> </div> <script> var div1 = document.getElementById("div1"); div1.addEventListener("clink", function(e) { var target = e.target; if (e.nodeName === "A") { alert(target.innerHrml); } }); </script> </body> </html>
if...else
或者switch..case
class User { constructor(type) { this.type = type; } buy() { if (this.type === "ordinary") { console.log("普通用戶購買"); } else if (this.type === "member") { console.log("會員購買"); } else if (this.type === "vip") { console.log("vip 用戶購買"); } } } // 測試代碼 var u1 = new User("ordinary"); u1.buy(); var u2 = new User("member"); u2.buy(); var u3 = new User("vip"); u3.buy();
上面代碼改進後node
class OridinaryUser { buy() { console.log("普通用戶購買"); } } class MemberUser { buy() { console.log("會員用戶購買"); } } class vipUser { buy() { console.log("vip用戶購買"); } } // 測試代碼 var u1 = new OridinaryUser("ordinary"); u1.buy(); var u2 = new MemberUser("member"); u2.buy(); var u3 = new vipUser("vip"); u3.buy();
class Action { handle() { handle1(); handle2(); handle3(); } handle1() { console.log("1"); } handle2() { console.log("2"); } handle3() { console.log("3"); } }
class Action { constructor(name) { this.name = name; this.nextAction = null; } setNextAction(action) { this.nextAction = action; } handle() { console.log(`${this.name} 審批`); if (this.nextAction != null) { this.nextAction.handle(); } } } let a1 = new Action("組長"); let a2 = new Action("經理"); let a3 = new Action("總監"); a1.setNextAction(a2); a2.setNextAction(a3); a1.handle();
js 中的鏈式操做es6
class Receive { exec() { console.log("執行"); } } class Command { constructor(recever) { this.receive = recever; } cmd() { console.log("觸發命令"); this.receive.exec(); } } class Invoker { constructor(command) { this.command = command; } invoke() { console.log("開始"); this.command.cmd(); } } //士兵 let solider = new Receive(); //小號手 let trumpter = new Command(solider); //將軍 let general = new Invoker(trumpter); general.invoke();
js 中的應用設計模式
document.exeCommand('bold')
document.exeCommand('undo')
// 狀態備忘 class Memento { constructor(content) { this.content = content; } getContent() { return this.content; } } // 備忘列表 class CareTaker { constructor() { this.list = []; } add(memento) { this.list.push(memento); } get(index) { return this.list[index]; } } //編輯器 class Editor { constructor() { this.content = null; } setContent(content) { this.content = content; } getContent() { return this.content; } saveContentToMemento() { return new Memento(this.content); } getContentFromMemento(memento) { this.content = memento.getContent(); } } //測試代碼 let editor = new Editor(); let careTaker = new CareTaker(); editor.setContent("111"); editor.setContent("222"); careTaker.add(editor.saveContentToMemento()); //存儲備忘錄 editor.setContent("333"); careTaker.add(editor.saveContentToMemento()); //存儲備忘錄 editor.setContent("444"); console.log(editor.getContent()); editor.getContentFromMemento(careTaker.get(1)); //撤銷 console.log(editor.getContent()); editor.getContentFromMemento(careTaker.get(0)); //撤銷 console.log(editor.getContent());
class Mediator { constructor(a, b) { this.a = a; this.b = b; } setA() { let number = this.b.number; this.a.setNumber(number * 100); } setB() { let number = this.a.number; this.b.setNumber(number / 100); } } class A { constructor() { this.number = 0; } setNumber(num, m) { this.number = num; if (m) { m.setB(); } } } class B { constructor() { this.number = 0; } setNumber(num, m) { this.number = num; if (m) { m.setA(); } } } let a = new A(); let b = new B(); let m = new Mediator(a, b); a.setNumber(100); console.log(a.number, b.number); //100 1 b.setNumber(100); console.log(a.number, b.number); //10000 100