<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ![](https://box.kancloud.cn/9ee5ae22a576e81dddd38dcd37034b95_633x215.png) ~~~ class State { constructor(color) { this.color = color } handle(context) { console.log(`turn to ${this.color} light`) context.setState(this) } } class Context { constructor() { this.state = null } setState(state) { this.state = state } getState() { return this.state } } // 測試代碼 let context = new Context() let green = new State('green') let yellow = new State('yellow') let red = new State('red') // 綠燈亮了 green.handle(context) console.log(context.getState()) // 黃燈亮了 yellow.handle(context) console.log(context.getState()) // 紅燈亮了 red.handle(context) console.log(context.getState()) ~~~ ~~~ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <p>有限狀態機</p> <button id="btn"></button> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="./03-javascript-state-machine.js"></script> <script> // 狀態機模型 var fsm = new StateMachine({ init: '收藏', // 初始狀態,待收藏 transitions: [ { name: 'doStore', from: '收藏', to: '取消收藏' }, { name: 'deleteStore', from: '取消收藏', to: '收藏' } ], methods: { // 執行收藏 onDoStore: function () { alert('收藏成功') updateText() }, // 取消收藏 onDeleteStore: function () { alert('已取消收藏') updateText() } } }) var $btn = $('#btn') // 點擊事件 $btn.click(function () { if (fsm.is('收藏')) { fsm.doStore(1) } else { fsm.deleteStore() } }) // 更新文案 function updateText() { $btn.text(fsm.state) } // 初始化文案 updateText() </script> </body> </html> ~~~ ~~~ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="./03-javascript-state-machine.js"></script> <script> // 模型 var fsm = new StateMachine({ init: 'pending', transitions: [ { name: 'resolve', from: 'pending', to: 'fullfilled' }, { name: 'reject', from: 'pending', to: 'rejected' } ], methods: { // 成功 onResolve: function (state, data) { // 參數:state - 當前狀態示例; data - fsm.resolve(xxx) 執行時傳遞過來的參數 data.successList.forEach(fn => fn()) }, // 失敗 onReject: function (state, data) { // 參數:state - 當前狀態示例; data - fsm.reject(xxx) 執行時傳遞過來的參數 data.failList.forEach(fn => fn()) } } }) // 定義 Promise class MyPromise { constructor(fn) { this.successList = [] this.failList = [] fn(() => { // resolve 函數 fsm.resolve(this) }, () => { // reject 函數 fsm.reject(this) }) } then(successFn, failFn) { this.successList.push(successFn) this.failList.push(failFn) } } // 測試代碼 function loadImg(src) { const promise = new MyPromise(function (resolve, reject) { var img = document.createElement('img') img.onload = function () { resolve(img) } img.onerror = function () { reject() } img.src = src }) return promise } var src = 'http://www.imooc.com/static/img/index/logo_new.png' var result = loadImg(src) console.log(result) result.then(function (img) { console.log('success 1') }, function () { console.log('failed 1') }) result.then(function (img) { console.log('success 2') }, function () { console.log('failed 2') }) </script> </body> </html> ~~~ ![](https://img.kancloud.cn/c8/2a/c82aff541d76a0b91968fd0532b64547_814x329.png)
                  <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>

                              哎呀哎呀视频在线观看