Search code examples
javascriptscopeeval

How to hide code argument during this code evaluation in specific scope?


Is it possible to make arg "code" inaccessible/invisible, while maintaining the availability of global objects (e.g. window and document) and arg "scope"? First and second log should display information while third one should thow an exception.

var foo;

function reg(scope, code) {
    eval(code);
}

reg(
    {test: "test"},
    'foo = function(){console.log("Window:", window); console.log("Scope:", scope); console.log("Code:", code);}'
);
foo();

Solution

  • You can unset that attribute inside eval:

    eval('code = undefined; ' + code);