如何在特定范围内的代码评估期间隐藏代码参数?

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

是否可以使 arg "code" inaccessible/invisible,同时保持全局对象(例如 window 和文档)和 arg "scope" 的可用性? 第一个和第二个日志应该显示信息,而第三个日志应该抛出异常。

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();

您可以在 eval 中取消设置该属性:

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