❗modify current scope at runtime❗️
🚧 施工中
JS ⟩ concepts ⟩ scope ⟩ modify current scope at runtime
eval("var a = 'aaa!';");
will introduce a new variable into the current scope. (I think "modify scope" is not accurate, "modify lexical environment" maybe❓) 👉 lexical environment vs. scope
will not introduce a new variable into the current scope, the newly created variable is local to the inner script. ( 👉 eval has its own scope in strict mode )
replit:modify scope at runtime
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!
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.
}
Last updated
Was this helpful?