使javascript变量在不同的函数中可见

Hov*_*ovo 3 javascript variables visibility scope

我需要在a某处声明变量,并使用javascript技术使其在f2函数内部被调用时可见f1.但是直接调用(f1函数外部)f2函数必须无法打印.我不能使用eval.我无法改变f2功能.我可以随意改变f1功能.这有可能吗?

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
Run Code Online (Sandbox Code Playgroud)

Sha*_*s M 5

小工作.声明全局并设置为undefined.a在f1中设置f2函数调用之前的值.a在f2调用后设置为undefined

var a = undefined;
function f1(var_name){
  a = 'this is a ';
  f2();
  a = undefined;
}
function f2(){
  console.log(a);
}
Run Code Online (Sandbox Code Playgroud)