🛡️eval has its own scope in strict mode

🚧 施工中

JSconceptsstrict mode ⟩ eval has its own scope in strict mode

in strict mode, script in eval has its own scope.

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)

👉 ⛔️ ReferenceError'xxx' is not defined❗️

Last updated