# function declaration instantiation

[JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [function](/web/js/val/func.md) ⟩ [declaration](/web/js/val/func/declare.md) ⟩ instantiation

{% hint style="success" %}
🚧&#x20;
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="success" %}
:star: [function declaration](/web/js/val/func/declare.md) <mark style="color:purple;">**instantiation**</mark>

When an [execution context](/web/js/concept/execution-context.md) is established for <mark style="color:yellow;">**evaluating**</mark> a [function](/web/js/val/func.md)：

* a <mark style="color:red;">**new**</mark> [Function Environment Record](https://tc39.es/ecma262/#sec-function-environment-records) is <mark style="color:yellow;">**created**</mark>.&#x20;
* <mark style="color:yellow;">**bindings**</mark> for each [parameter](/web/js/val/func/param.md) are <mark style="color:yellow;">**instantiated**</mark> in that [Environment Record](https://tc39.es/ecma262/#sec-environment-records).&#x20;
* <mark style="color:green;">**body declarations**</mark> are also instantiated：
  * :star: if the <mark style="color:yellow;">**parameters**</mark> <mark style="color:red;">**do not**</mark>**&#x20;**<mark style="color:yellow;">**include**</mark> any <mark style="color:yellow;">**default value initializers**</mark>： \
    the <mark style="color:green;">**body declarations**</mark> are instantiated in the <mark style="color:red;">**same**</mark> [Environment Record](https://tc39.es/ecma262/#sec-environment-records) as the <mark style="color:orange;">**parameters**</mark>:exclamation:(:point\_right: [parameter scope](/web/js/scope/parameter-scope.md))
  * :star: if <mark style="color:yellow;">**default value initializers**</mark> <mark style="color:red;">**exist**</mark>：

    a <mark style="color:red;">**second**</mark> [Environment Record](https://tc39.es/ecma262/#sec-environment-records) is created for the <mark style="color:green;">**body declarations**</mark>:exclamation:
* [parameter](/web/js/val/func/param.md)(s) / [function](/web/js/val/func.md)(s) are initialized as <mark style="color:yellow;">**part**</mark> of [FunctionDeclarationInstantiation](https://tc39.es/ecma262/#sec-functiondeclarationinstantiation). All <mark style="color:yellow;">**other bindings**</mark> are initialized during <mark style="color:yellow;">**evaluation**</mark> of the <mark style="color:yellow;">**function body**</mark>.

📘 [JS spec](/web/master/ref/js-spec.md) ⟩ [FunctionDeclarationInstantiation](https://tc39.es/ecma262/#sec-functiondeclarationinstantiation)
{% endhint %}
{% endtab %}

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

* [redeclaration](/web/js/grammar/declare/redeclare.md)
* [variable redeclaration](/web/js/variable/redeclare.md)
* [simple vs. non-simple parameter](/web/js/val/func/param/simple-vs-nonsimple.md) - <mark style="color:yellow;">**determines**</mark> whether <mark style="color:blue;">**parameters**</mark> and <mark style="color:green;">**body declarations**</mark> are <mark style="color:yellow;">**stored**</mark> in the <mark style="color:red;">**same**</mark> <mark style="color:yellow;">**environment record**</mark>.
* [parameter scope](/web/js/scope/parameter-scope.md) - the scope where all the parameters live.
* [execution context](/web/js/concept/execution-context.md) ⟩ [environment record](/web/js/concept/execution-context/lexical-environment/environment-record.md)
  {% endtab %}

{% tab title="📘 手冊" %}

* [JS spec](/web/master/ref/js-spec.md) ⟩ [FunctionDeclarationInstantiation](https://tc39.es/ecma262/#sec-functiondeclarationinstantiation)
  {% 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) ⟩ Appendix A
  {% endtab %}

{% tab title="❓" %}
{% hint style="warning" %}
Q：

* does "evaluating a function" mean "calling a function"❓
* or, it means "evaluating the function itself" (to be a "function" object)❓
  {% endhint %}

{% hint style="info" %}
[execution context](/web/js/concept/execution-context.md) 是 [runtime](/web/js/compile/runtime.md) 的才有的動作，但 evaluating the function to be a "function" object 是 compile-time 的問題，所以我猜答案應該是第一個吧❓
{% endhint %}
{% endtab %}

{% tab title="🚧" %}

* [ ] binding
* [ ] default value initializer
* [ ] function environment record
  {% 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/declare/instantiation.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.
