> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/concept/env/js-engine/mode/sloppy-mode/modify-current-scope-at-runtime.md).

# modify current scope at runtime❗️

🚧 施工中

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

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

{% hint style="danger" %}
[sloppy mode](/web/js/concept/env/js-engine/mode/sloppy-mode.md)：

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

{% hint style="success" %}
[strict mode](/web/js/concept/env/js-engine/mode/strict-mode.md)：

* 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](/web/js/concept/env/js-engine/mode/strict-mode/eval-has-its-own-scope-in-strict-mode.md) )
  {% 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](/web/js/concept/env/js-engine/mode/strict-mode/eval-has-its-own-scope-in-strict-mode.md)
* [lexical environment](/web/js/concept/execution-context/lexical-environment.md)
* [lexical environment vs. scope](/web/js/scope/lexical-environment-vs.-scope.md)
  {% 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. 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 %}
