Javascript - 在 context/scope 中求值,不使用 with 语句不使用此关键字

Javascript - eval in context/scope without using this keyword without with statement

此 post 提供了如何在特定 context/scope 中使用 eval 的解决方案,而无需在 eval 代码中的所有 variables/functions 前面加上 this

澄清一下 - 假设我有以下对象作为 eval 语句 {dog: "labrador"} 的上下文。我希望 console.log(eval(dog)) 输出 "labrador" 而不必输入 eval(this.dog)

然而,链接post中的解决方案:

function evalInContext(scr, context)
{
    // execute script in private context
    return (new Function( "with(this) { return " + scr + "}")).call(context);
}

使用 with 语句。考虑到不鼓励使用 with 语句,是否有替代解决方案?

is there an alternative solution?

当然可以,但这比 with 更糟糕:

 return new Function(...Object.keys(context), scr)(...Object.values(context));