> 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/142han-shu-de-you-hua/1423duo-xing-zai-ru-han-shi.md).

# 1.4.2.3.惰性載入函式

* 使用時機
  * 有些時候不需執行函式中的某些判斷, 或是函式中的內容會依使用時間做調整, 例如第一次使用此函式, 會與第二次後使用此函式, 函式內容會做變動
* example code
  * 1.普通函數: 缺點： 每次使用時addEvent時皆會進入判斷式

    [![](https://3195929128-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4M0G8GO2MkGtHfarAf%2F-M4M0IHoyFvfGKjf7P-T%2F-M4M0MN0A9n8LlLJwCZ0%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-03-25%20%E4%B8%8B%E5%8D%888.46.56.png?generation=1586302920637156\&alt=media)](https://codepen.io/JenHsuan/pen/qrydRB?editors=0011)
  * 2.即刻調用的函式運算式：程式碼載入時就立即進入判斷式, 缺點: 當沒有使用該函式時仍會進行一次判斷

    [![](https://3195929128-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4M0G8GO2MkGtHfarAf%2F-M4M0IHoyFvfGKjf7P-T%2F-M4M0MN2KfdK63gH-SMt%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-03-25%20%E4%B8%8B%E5%8D%888.47.08.png?generation=1586302920896954\&alt=media)](https://codepen.io/JenHsuan/pen/qrydRB?editors=0011)
  * 3.普通函數: 一進行判斷後, 便改寫自己 -> 只要call一次, 後面就不需要進入判斷式

    [![](https://3195929128-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4M0G8GO2MkGtHfarAf%2F-M4M0IHoyFvfGKjf7P-T%2F-M4M0MN43BJM7xiiuksS%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-03-25%20%E4%B8%8B%E5%8D%888.47.19.png?generation=1586302920774442\&alt=media)](https://codepen.io/JenHsuan/pen/qrydRB?editors=0011)
