---
description: 🚧 施工中
---

# modify current scope at runtime❗️

[JS](../../../../..) ⟩ [concepts](../../../../../../browser/concepts) ⟩ [scope](../../../../../scope) ⟩ modify current scope at runtime

```javascript
eval("var a = 'aaa!';");
```

{% hint style="danger" %}
[](<> "mention")：

* will <mark style="color:yellow;">**introduce**</mark> a <mark style="color:red;">**new variable**</mark> into the <mark style="color:yellow;">**current**</mark> [scope](../../../../../scope "mention").\
  (I think "modify scope" is not accurate, "modify [lexical-environment](../../../../execution-context/lexical-environment "mention")" maybe:question:) \
  :point\_right: [lexical-environment-vs.-scope](../../../../../scope/lexical-environment-vs.-scope "mention")
{% endhint %}

{% hint style="success" %}
[strict-mode](../strict-mode "mention")：

* will <mark style="color:red;">**not**</mark> <mark style="color:yellow;">**introduce**</mark> a new variable into the current scope, the newly created variable is local to the inner script. ( :point\_right: [eval-has-its-own-scope-in-strict-mode](../strict-mode/eval-has-its-own-scope-in-strict-mode "mention") )
{% endhint %}

{% tabs %}
{% tab title="💈範例" %}
* replit：[modify scope at runtime](https://replit.com/@pegasusroe/modify-scope-at-runtime#index.js)

```javascript
function modifyScope() {
    
    // ⭐️ modify current (function) scope at runtime
    eval("var a = 'aaa!';");
    //        ╰── `a` is in "function scope"❗
    
    console.log(a);    // `a` is visible.
}

modifyScope();                       // aaa!
```

* replit：[modify scope at runtime (with)](https://replit.com/@pegasusroe/modify-scope-at-runtime-with#index.js)

```javascript
var scope = { name: "Joy" };

// ⭐️ `with`
// -------------------------------------------------------
// turns an object into a local scope, 
// its properties are treated as identifiers in the block:
with (scope) {
    console.log(name);   // Joy
    //          ^^^^     // `name` is visible.
}
```
{% endtab %}

{% tab title="👥 相關" %}
* [eval-has-its-own-scope-in-strict-mode](../strict-mode/eval-has-its-own-scope-in-strict-mode "mention")
* [lexical-environment](../../../../execution-context/lexical-environment "mention")
* [lexical-environment-vs.-scope](../../../../../scope/lexical-environment-vs.-scope "mention")
{% endtab %}

{% tab title="📗 參考" %}
* [ ] [ydkjs-scope-and-closures-v.2](../../../../../../master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2 "mention") ⟩ Ch. 1 ⟩ [Runtime Scope Modifications](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch1.md#cheating-runtime-scope-modifications)
{% endtab %}

{% tab title="🚧" %}
* [ ] with
{% endtab %}
{% endtabs %}
