> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/chapter1/11bi-ji/14function/141callback-function.md).

# 1.4.1.Callback function

* 將函式作為參數傳遞, 借由這個特性我們可以抽離一部份的商業邏輯, 將這個商務邏輯放到函式參數中, 可以分離程式碼中變化與不變的部份
* 1.使用時機
  * 1.將function當成參數代入另一個function中
  * 2.當要接續某個function執行完後,再執行另一個function, 例如ajax
* 2.架構
  * ref: <http://fireqqtw.logdown.com/posts/258823-javascript-function-notes>

    ```
    function two(callback) {
      var count1 = 1;
      (callback && typeof(callback) === "function") && callback();
      console.log(count1);
    }

    function three() {
      console.log('three run');
    }

    two(function() {
      console.log('callback run');
    });

    two(three);
    ```
* 3.範例

  * 1.<https://codepen.io/JenHsuan/pen/qrYYgB?editors=0011>

  ![](https://3195929128-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4M0G8GO2MkGtHfarAf%2F-M4M0IHoyFvfGKjf7P-T%2F-M4M0MJEDbkuXvIFR4ZH%2F%E6%9C%AA%E5%91%BD%E5%90%8D.jpg?generation=1586302920541269\&alt=media)

  * 2.[currying](https://codepen.io/JenHsuan/pen/gmKmNg?editors=0012#)
