[英]How to get the variables that were created inside a function?
我正在node.js中执行javascript文件,我需要访问在该文件中创建的所有变量。 由于这些javascript文件可以包含任何内容,具体取决于开发人员上下文,我需要以编程方式访问变量。
我的问题是:如何获取函数内创建的变量? 像这样的东西:
function test(){
var a = 'hello world';
var b = 100;
}
console.log(test.variables);
// -> { "a": 'hello world', "b": 100 }
这有可能吗?
不,您需要在对象中返回这些值。
function test(){
var a = 'hello world',
b = 100;
return {
a: a,
b: b
};
}
console.log(test); // { "a": 'hello world', "b": 100 }
或者,您可以将这些值保存到函数范围之外的变量:
var variables = null;
function test(){
var a = 'hello world',
b = 100;
variables = {
a: a,
b: b
};
}
console.log(variables); // { "a": 'hello world', "b": 100 }
这有可能吗?
不 - 不是没有改变功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.