Search lands in PR-5.1 (Pagefind).

How-to Advanced

Chapter 12 Updated

Polyfills, this/call/apply/bind, Currying, Debounce, Promises & Coding Challenges

Polyfills, this/call/apply/bind, currying, debounce, deep clone — code these live.

  • Full 12m
  • Revision 3m
  • Flow 2m

The closure family tree — one pattern, many problems

flowchart TD
    C["<b>i · Closure primitive</b><br/>outer fn returns inner fn<br/>inner closes over private state"]
      --> RL["<b>ii · Rate limiters</b><br/>private state = timer / flag"]
    C --> IG["<b>iii · Invocation gates</b><br/>private state = cache / called"]
    C --> FT["<b>iv · Functional transformers</b><br/>private state = accumulated args"]
    RL --> DB["<b>v · Debounce</b><br/>clearTimeout + setTimeout<br/>last call wins"]
    RL --> TH["<b>vi · Throttle</b><br/>boolean flag + setTimeout<br/>first call per interval"]
    IG --> ON["<b>vii · Once</b><br/>boolean called + cached result"]
    IG --> MEM["<b>viii · Memoize</b><br/>Map cache keyed by JSON.stringify(args)"]
    FT --> CU["<b>ix · Curry</b><br/>collect args until fn.length reached"]
    FT --> PC["<b>x · Pipe / Compose</b><br/>reduce / reduceRight through fns"]
 
    classDef root   fill:#fdecd3,stroke:#c2410c,stroke-width:2px,color:#1a1915;
    classDef branch fill:#f5efe1,stroke:#6a8a4f,stroke-width:2px,color:#1a1915;
    classDef leaf   fill:#e7efd9,stroke:#587640,stroke-width:2px,color:#1a1915;
    class C root
    class RL,IG,FT branch
    class DB,TH,ON,MEM,CU,PC leaf
  • Closure primitive

    Every pattern here returns a new function that closes over some private state — timers, flags, caches, or accumulated args.

  • Rate limiters

    Control how often a function can fire. Private state is a timer handle or a boolean flag.

  • Invocation gates

    Control whether the function runs at all. Private state is a called flag or a results cache.

  • Functional transformers

    Reshape how a function is called. Private state is either accumulated args (curry) or an ordered list of functions (pipe/compose).

  • Debounce

    clearTimeout + setTimeout — runs fn delay ms after the LAST call. Good for search-as-you-type.

  • Throttle

    Boolean `inThrottle` flag — runs fn at most once per interval. Good for scroll/resize.

  • Once

    Runs exactly once; subsequent calls return the cached result. Useful for init paths and "pay once" flows.

  • Memoize

    Cache keyed by JSON.stringify(args). Skips recomputation for identical inputs.

  • Curry

    Collect args across calls; invoke the original once args.length ≥ fn.length. Enables partial application.

  • Pipe / Compose

    Thread a value through a list of functions. pipe goes left→right (reduce); compose goes right→left (reduceRight).

Comments

Comments are disabled in this environment. Set PUBLIC_GISCUS_REPO, PUBLIC_GISCUS_REPO_ID, and PUBLIC_GISCUS_CATEGORY_ID to enable.