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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ## 說明 函數可以像對待任何其他數據類型一樣對待它們——把它們存在數組里,當作參數傳遞,賦值給變量...等等 ## 例子 ### 例子1 bad ``` const hi = name => `Hi ${name}`; const greeting = name => hi(name); ``` good ``` const hi = name => `Hi ${name}`; const greeting = hi =; ``` ### 例子2 用一個函數把另一個函數包起來,目的僅僅是延遲執行,真的是非常糟糕的編程習慣 ``` // 太傻了 const getServerStuff = callback => ajaxCall(json => callback(json)); // 等價于 ajaxCall(json => callback(json)); // 等價于 ajaxCall(callback); // 等價于 const getServerStuff = callback => ajaxCall(callback); // 等價于(最終) const getServerStuff = ajaxCall ``` ### 例子3 bad ``` const BlogController = { index(posts) { return Views.index(posts); }, show(post) { return Views.show(post); }, create(attrs) { return Db.create(attrs); }, update(post, attrs) { return Db.update(post, attrs); }, destroy(post) { return Db.destroy(post); }, }; ``` good ``` const BlogController = { index: Views.index, show: Views.show, create: Db.create, update: Db.update, destroy: Db.destroy, }; ``` ## 函數是一等公民的好處 - 如果一個函數被不必要的包裹起來,那么函數變動時,被包裹的也需要變動 bad ``` httpGet('/post/2', json => renderPost(json)); // 改變 httpGet('/post/2', (json, err) => renderPost(json, err)); ``` good ``` httpGet('/post/2', renderPost); // renderPost 將會在 httpGet 中調用,想要多少參數都行 ``` - 增加函數的通用性 ``` // 只針對當前的博客 const validArticles = articles => articles.filter(article => article !== null && article !== undefined), // 對未來的項目更友好 const compact = xs => xs.filter(x => x !== null && x !== undefined); ``` - 在 js 中 需要注意 this ``` var fs = require('fs'); // 太可怕了 fs.readFile('freaky_friday.txt', Db.save); // 好一點點 fs.readFile('freaky_friday.txt', Db.save.bind(Db)); ``` > 把 Db 綁定(bind)到它自己身上以后,你就可以隨心所欲地調用它的原型鏈式垃圾代碼了
                  <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>

                              哎呀哎呀视频在线观看