You are looking for the worst hack imaginable? Sure, everything is possible:
function example () {
with(horrible(this)) {
var hello = 'hi';
}
}
var x = new example;
x.hello; // 'hi'
function horrible(target) {
return new Proxy(target, {
has() { return true; }, // contains all variables you could ever wish for!
get(_, k) { return k in target ? target[k] : (1,eval)(k) }
});
}
The proxy claims to contain all names that could ever be used as a variable in the with
scope. This basically leads to all assignments of undeclared or var
-declared variables create properties on the target (unless you use let
or const
, they'll be truly local to the block scope).
However, for variable lookup everything that is not a target property will be resolved in the global scope (using global eval
) as the original scope cannot be retained when the proxy said it can deliver all variables.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…