admin管理员组文章数量:1427548
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 howf2
accessesa
or making all calls tof2
have access toa
. You might be able to make a copy off2
though and modify it. – Kevin B Commented Sep 29, 2015 at 15:52
3 Answers
Reset to default 5Small 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.
本文标签: make javascript variable visible in different functionsStack Overflow
版权声明:本文标题:make javascript variable visible in different functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745500409a2661003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论