【发布时间】:2013-06-26 14:08:44
【问题描述】:
我想动态创建一个局部变量。 JavaScript: Dynamically Creating Variables for Loops 并不是我想要的。我不想要一个数组。我想像访问局部变量一样访问它。
类似:
<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>
这行得通。但我正在寻找替代/更好的解决方案。不用eval可以吗?
编辑: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>
【问题讨论】:
-
为什么?很难知道您要解决的真正问题是什么,但很容易猜测这是错误的解决方案。
-
如果在函数中使用 var 关键字声明任何变量都是局部变量。尽量不要使用 eval,这确实看起来非常复杂。
-
只是为了澄清这个问题:您是否试图在函数“内部”时在外部或全局范围内声明变量?或者您是否尝试将变量添加到函数调用的上下文中(这就是
this关键字的用途)? -
我想在调用 createVariables() 函数的地方声明局部变量。我不想要全局变量。
-
@NimChimpsky:当然这很复杂,因为它是一个丑陋的解决方法。你能用更清楚的方式来做吗?我对此表示怀疑.. 另一方面,我不明白为什么我会这样做..
标签: javascript