I need to declare the variable a somewhere, and using javascript technics make it visible for f2 function being called inside the f1 function. But being called directly (outside of the f1 function) the f2 function must fail to print a.
I can't use eval.
I can't change the f2 function.
I can change the f1 function however I want.
Is that possible at all?
function f1(var_name){
f2();
}
function f2(){
console.log(a);
}
f1(); // must log value of the a
f2(); // must not be able to log a
awould not function as OP stated.f2will only have access to the scopes above it. you can't inject a variable into it other than through the use of .call/apply or arguments, neither of which would work without modifying howf2accessesaor making all calls tof2have access toa. You might be able to make a copy off2though and modify it.