我想动态创建一个局部变量。JavaScript:为循环动态创建变量并不是我想要的。我不想要一个数组。我想像访问局部变量一样访问它。
就像是:
<script type="text/javascript">
var properties = new Object();
properties["var1"] = "value1";
properties["var2"] = "value2";
createVariables(properties);
function createVariables(properties)
{
// This function should somehow create variables in the calling function. Is there a way to do that?
}
document.write("Outside the function : " + var1 + "<br>");
document.write("Outside the function : " + var2 + "<br>");
</script>
我尝试了以下代码。
<script type="text/javascript">
var properties = new Object();
properties["var1"] = "value1";
properties["var2"] = "value2";
createVariables(properties);
function createVariables(properties)
{
for( var variable in properties)
{
try
{
eval(variable);
eval(variable + " = " + properties[variable] + ";");
}
catch(e)
{
eval("var " + variable + " = '" + properties[variable] + "';");
}
}
document.write("Inside the function : " + var1 + "<br>");
document.write("Inside the function : " + var2 + "<br>");
}
document.write("Outside the function : " + var1 + "<br>");
document.write("Outside the function : " + var2 + "<br>");
</script>
但是在 createVariables() 之外无法访问生成的变量。
现在,我有这个解决方案。
<script type="text/javascript">
var properties = new Object();
properties["var1"] = "value1";
properties["var2"] = "value2";
function createVariables(properties)
{
var str = "";
for( var variable in properties)
{
str += "try{";
str += "eval('" + variable + "');";
str += "eval(\"" + variable + " = properties['" + variable + "'];\");";
str += "}";
str += "catch(e){";
str += "eval(\"var " + variable + " = properties['" + variable + "'];\");";
str += "}";
}
return str;
}
eval(createVariables(properties));
document.write("Outside the function : " + var1 + "<br>");
document.write("Outside the function : " + var2 + "<br>");
</script>
这行得通。但我正在寻找替代/更好的解决方案。是否可以在没有评估的情况下做到这一点?
编辑:7 月 4 日
你好,
我尝试了类似于@Jonathan 建议的解决方案。
<script type="text/javascript">
var startFunc = function(){
var self = this;
self.innerFunc = function innerFunc(){
var properties = new Object();
properties["var1"] = "value1";
properties["var2"] = "value2";
properties["var3"] = "value3";
function createVariables(caller, props) {
for(i in props) {
caller[i] = props[i];
}
caller.func1();
}
createVariables(self, properties);
console.log( var1 );
}
self.func1 = function func1(){
console.log( "In func 1" );
console.log( var2 );
}
innerFunc();
console.log( var3 );
}
startFunc();
</script>
这一切都很好。但它实际上是在创建全局变量,而不是在函数中创建变量。
传递给 createVariables() 函数的“self”是 window. 我不确定为什么会这样。我将功能范围分配给自我。我不确定这里发生了什么。在这种情况下,无论如何都要创建全局变量。
如果我的问题不清楚,
我所追求的是在调用者中创建局部变量。场景就像
1)我在一个函数里面。
2)我调用另一个函数,它返回给我一个地图[这个地图包含一个变量的名称和值]。
3)我想动态创建所有变量,如果它们尚未定义。如果它们已经定义 [global/local],我想更新它们。
4)一旦创建了这些变量,我应该能够在没有任何上下文的情况下访问它们。[只是变量名]
<script type="text/javascript">
function mainFunc()
{
var varibalesToBeCreated = getVariables();
createVariables(varibalesToBeCreated);
alert(var1);
alert(var2);
}
function createVariables(varibalesToBeCreated)
{
// How can I implement this function,
// such that the variables are created in the caller?
// I don't want these variables this function.
}
function getVariables()
{
var properties = new Object();
properties["var1"] = "value1";
properties["var2"] = "value2";
}
mainFunc();
</script>