# decorator

* [JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [function](/web/js/val/func.md) ⟩ decorator
* [JS](/web/js.md) ⟩ [technique](/web/js/tech.md) ⟩ decorator

{% hint style="success" %}
([function](/web/js/val/func.md)) <mark style="color:yellow;">**alters**</mark>/<mark style="color:yellow;">**extends**</mark> the behavior of <mark style="color:yellow;">**another function**</mark>.
{% endhint %}

{% tabs %}
{% tab title="🔴 主題" %}

* [saveCallHistory(f)](/web/js/val/func/kind/higher/decorator/savecallhistory-f.md) - make function save its call history ("arguments").
* [memoize(f)](/web/js/val/func/kind/higher/decorator/memoize/memoize-by-decorator/memoize-f.md) - make function "remember" its "return values".
* [benchmark(f)](/web/js/val/func/kind/higher/decorator/benchmark-f.md)
* [delay(f, s)](/web/js/val/func/kind/higher/decorator/delay-f-s.md) - delay the execution of a function by s seconds.
* [debounce(f, s)](/web/js/val/func/kind/higher/decorator/debounce-f-s.md) - debounce function calls for s seconds.
* [throttle(f, s)](/web/js/val/func/kind/higher/decorator/throttle-f-s.md) - throttle function calls by every s seconds.
* [debounce vs. throttle](/web/js/val/func/kind/higher/decorator/debounce-vs.-throttle.md) - difference between the two.
  {% endtab %}

{% tab title="🧨 雷區" %}
{% hint style="warning" %}

* if the <mark style="color:yellow;">**original function**</mark> had <mark style="color:red;">**properties**</mark> on it, like <mark style="color:blue;">`func.calledCount`</mark> or whatever, then the <mark style="color:purple;">**decorated**</mark>**&#x20;**<mark style="color:yellow;">**one**</mark> will <mark style="color:red;">**not**</mark> provide them:exclamation:
* there's a way to create <mark style="color:purple;">**decorators**</mark> that <mark style="color:yellow;">**keep access**</mark> to <mark style="color:yellow;">**function properties**</mark>, but this requires using a special `Proxy` object to wrap a function.  👉 [Proxy and Reflect](https://javascript.info/proxy#proxy-apply).
  {% endhint %}
  {% endtab %}

{% tab title="👥 相關" %}

* [memoize by decorator](/web/js/val/func/kind/higher/decorator/memoize/memoize-by-decorator.md)
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] JS.info ⟩ [Decorators and forwarding, call/apply](https://javascript.info/call-apply-decorators) ⭐️
  {% endtab %}

{% tab title="🚧" %}

* benchmark(f) - timed version of a function.
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/val/func/kind/higher/decorator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
