# closure: to close over or not

[JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [function](/web/js/val/func.md) ⟩ [closure](/web/js/val/func/closure.md) ⟩ [example](/web/js/val/func/closure/examples.md) ⟩ to close over or not:question:

{% hint style="info" %}
even an <mark style="color:yellow;">**outer variable**</mark> is <mark style="color:red;">**not**</mark>**&#x20;**<mark style="color:yellow;">**mentioned explicitly**</mark> in a closure, it is <mark style="color:yellow;">**still being closed over**</mark> by the closure:exclamation:
{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}

* replit：[to close over or not?](https://replit.com/@pegasusroe/closure-to-close-over-or-not#index.js)

```javascript
// • returns a closure
// • parameters are closed over
function GetInfo(id, name, grade) {
    // ⭐ closure
    // although `id`, `name`, `grade` are not mentioned explicitly,
    // these parameters are still being closed over by the closure.
    return function getInfo(whichValue) {
        // ⭐ eval('id') returns `id`, etc ...
        return eval(whichValue);
    };
}

const info = GetInfo(73, "Suzy", 87);
info("name"),    // Suzy
info("grade"),   // 87
```

{% endtab %}

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

* [YDKJS: Scope & Closures (v.2)](/web/master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2.md) > Ch.7 > [Per Variable or Per Scope?](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch7.md#per-variable-or-per-scope)
  {% 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/closure/examples/closure-to-close-over-or-not.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.
