# modify current scope at runtime❗️

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [concepts](https://lochiwei.gitbook.io/web/browser/concepts) ⟩ [scope](https://lochiwei.gitbook.io/web/js/scope) ⟩ modify current scope at runtime

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

{% hint style="danger" %}
[](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/sloppy-mode "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](https://lochiwei.gitbook.io/web/js/scope "mention").\
  (I think "modify scope" is not accurate, "modify [lexical-environment](https://lochiwei.gitbook.io/web/js/concept/execution-context/lexical-environment "mention")" maybe:question:) \
  :point\_right: [lexical-environment-vs.-scope](https://lochiwei.gitbook.io/web/js/scope/lexical-environment-vs.-scope "mention")
  {% endhint %}

{% hint style="success" %}
[strict-mode](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/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](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/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](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/strict-mode/eval-has-its-own-scope-in-strict-mode "mention")
* [lexical-environment](https://lochiwei.gitbook.io/web/js/concept/execution-context/lexical-environment "mention")
* [lexical-environment-vs.-scope](https://lochiwei.gitbook.io/web/js/scope/lexical-environment-vs.-scope "mention")
  {% endtab %}

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

* [ ] [ydkjs-scope-and-closures-v.2](https://lochiwei.gitbook.io/web/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 %}
