🛡️eval has its own scope in strict mode
🚧 施工中
JS ⟩ concepts ⟩ strict mode ⟩ eval has its own scope in strict mode
in strict mode, script in eval has its own scope.
replit:modify scope at runtime
function evalCannotModifyScopeInStrictMode() {
'use strict'; // ⭐️ strict mode
// ---------------------------------------------------------------
// ⭐️ in strict mode, `eval` doesn't introduce new variables into
// the surrounding scope, they're local to the script itself.
eval("var b = 'bbb!';");
// ╰── `b` is "local" to the inner script❗
// ---------------------------------------------------------------
console.log(b); // `b` is invisible.
// ^
// ⛔ ReferenceError: `b` is not defined
}
evalCannotModifyScopeInStrictMode(); // ⛔ ReferenceError (runtime error)
Last updated
Was this helpful?