<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                >[success] # es5 的解決方案 ~~~ 1.將對象放入另一個對象之中,這些對象以一條鏈的方式進行引用,形成一個聚合對象。 這些對象都擁有相同的接口,當請求達到鏈中的某個對象時,這個對象會執行自身的操作, 隨后把請求轉發給鏈中的 下一個對象。 2.第二種利用'js' 動態語言的特性直接改代碼 3.第三種AOP裝飾函數 ~~~ >[danger] ##### 第一種方式對象傳遞形成鏈的方式 ~~~ 1.首先第一種方式我們將當前對象傳遞給另一個對象,兩個對象都有相同的方法,這樣用起來 對用戶來說是透明的,像下面的案例'MissileDecorator' 和'AtomDecorator' 都有'fire'方法因此 只不過這兩個類中的'fire' 方法會將我們'Plane'的'fire' 方法進行加工 ~~~ ~~~ var Plane = function(){} Plane.prototype.fire = function(){ console.log( '發射普通子彈' ); } // 接下來增加兩個裝飾類,分別是導彈和原子彈: var MissileDecorator = function( plane ){ this.plane = plane; } MissileDecorator.prototype.fire = function(){ this.plane.fire(); console.log( '發射導彈' ); } var AtomDecorator = function( plane ){ this.plane = plane; } AtomDecorator.prototype.fire = function(){ this.plane.fire(); console.log( '發射原子彈' ); } var plane = new Plane(); plane = new MissileDecorator( plane ); plane = new AtomDecorator( plane ); plane.fire(); // 分別輸出: 發射普通子彈、發射導彈、發射原子彈 ~~~ >[danger] ##### js 動態語言的特性 ~~~ 1.這個思路就是先存起來之前的要被裝飾的方法,然后重寫要被裝飾的方法并且將之前保存的該方法 放入在接著增加想要擴展的邏輯 ~~~ ~~~ var plane = { fire: function(){ console.log( '發射普通子彈' ); } } var missileDecorator = function(){ console.log( '發射導彈' ); } var atomDecorator = function(){ console.log( '發射原子彈' ); } var fire1 = plane.fire; plane.fire = function(){ fire1(); missileDecorator(); } // 保存一下原來的plane中fire邏輯 var fire2 = plane.fire; // 直接給原來的plane中fire重寫并將上次記錄原來的fire邏輯放回來 plane.fire = function(){ fire2(); atomDecorator(); } plane.fire(); // 分別輸出: 發射普通子彈、發射導彈、發射原子彈 ~~~ >[danger] ##### 第三種AOP裝飾函數 ~~~ Function.prototype.before = function( beforefn ){ var __self = this; // 保存原函數的引用 return function(){ // 返回包含了原函數和新函數的"代理"函數 beforefn.apply( this, arguments ); // 執行新函數,且保證 this 不被劫持,新函數接受的參數 // 也會被原封不動地傳入原函數,新函數在原函數之前執行 return __self.apply( this, arguments ); // 執行原函數并返回原函數的執行結果, // 并且保證 this 不被劫持 } } Function.prototype.after = function( afterfn ){ var __self = this; return function(){ var ret = __self.apply( this, arguments ); afterfn.apply( this, arguments ); return ret; } }; ~~~ * 使用的小案例 ~~~ <html> <button tag="login" id="button">點擊打開登錄浮層</button> <script> Function.prototype.after = function( afterfn ){ var __self = this; return function(){ var ret = __self.apply( this, arguments ); afterfn.apply( this, arguments ); return ret; } }; var showLogin = function(){ console.log( '打開登錄浮層' ); } var log = function(){ console.log( '上報標簽為: ' + this.getAttribute( 'tag' ) ); } showLogin = showLogin.after( log ); // 打開登錄浮層之后上報數據 document.getElementById( 'button' ).onclick = showLogin; </script> </html> ~~~ * 表單驗證的案例 ~~~ Function.prototype.before = function( beforefn ){ var __self = this; return function(){ if ( beforefn.apply( this, arguments ) === false ){ // beforefn 返回 false 的情況直接 return,不再執行后面的原函數 return; } return __self.apply( this, arguments ); } } var validata = function(){ if ( username.value === '' ){ alert ( '用戶名不能為空' ); return false; } if ( password.value === '' ){ alert ( '密碼不能為空' ); return false; } } var formSubmit = function(){ var param = { username: username.value, password: password.value } ajax( 'http:// xxx.com/login', param ); } // 裝飾器 formSubmit = formSubmit.before( validata ); submitBtn.onclick = function(){ formSubmit(); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看