最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

make javascript variable visible in different functions - Stack Overflow

programmeradmin1浏览0评论

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

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
Share Improve this question asked Sep 29, 2015 at 15:47 HovoHovo 7904 silver badges21 bronze badges 11
  • Why not just declare it globally? – Manu Masson Commented Sep 29, 2015 at 15:48
  • @Manu then the last line which says it must not be able to log a would not function as OP stated. – 1252748 Commented Sep 29, 2015 at 15:49
  • why not use getter and setters? – Jorge Y. C. Rodriguez Commented Sep 29, 2015 at 15:49
  • 1 Please look for and read about XY problem – Amit Commented Sep 29, 2015 at 15:50
  • 2 No, not possible, f2 will 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 how f2 accesses a or making all calls to f2 have access to a. You might be able to make a copy of f2 though and modify it. – Kevin B Commented Sep 29, 2015 at 15:52
 |  Show 6 more ments

3 Answers 3

Reset to default 5

Small work around. Declare a globally and set to undefined. Set the value of a before f2 function call inside f1. Set a to undefined after f2 call

var a = undefined;
function f1(var_name){
  a = 'this is a ';
  f2();
  a = undefined;
}
function f2(){
  console.log(a);
}

why not using another global variable ? You define a global variable a and in the function f1 you declare a new global variable b = a, call the f2 function that will print the b global variable, set the gobal variable b to NULL again.

With this, b is gonna be defined only during the f1 function and would have the value of the global a variable.

This way will work only if f2() uses "this" already: (in such case there will not be changes to add "this" support).

function f1(var_name){
    var scope = {a: var_name};
    f2.call(scope);
}
function f2(){
    console.log(this.a);
}

f1(123);  // must log value of the a
f2();  // must not be able to log a

Also you could think about functions overloading.

发布评论

评论列表(0)

  1. 暂无评论