问题描述
这可能是一个全新的问题,但我很难过。
如果我有这样的功能as:
函数DoSomething(strVarName){
.....
}
我称之为:
DoSomething(''myVarName'');
在函数内部,怎么做我为名为myVarName的变量赋值了一个值
(即名称为strVarName值的变量)?
谢谢!
Russ
Hi,
This may be a totally newbie question, but I''m stumped.
If I have a function such as:
function DoSomething(strVarName) {
.....
}
and I call it like so:
DoSomething(''myVarName'');
Inside the function, how do I assign a value to the variable named myVarName
(i.e. the variable whose name is the value of strVarName)?
Thanks!
Russ
推荐答案
window [strVarName] =某个值;
Mi ck
window[strVarName]=some value;
Mick
window [strVarName] =某个值;
window[strVarName]=some value;
不,不要'' T。 'window''可能是许多
案例中对全局对象的引用,但并非全部。改为使用对全局对象的引用:
var MyVarName = ...;
var _global = this;
函数doSomething(strVarName)
{
... _global [strVarName] ...
}
doSomething(''myVarName'');
如果你担心新的全球`_global'会破坏你的全球
命名空间,你也可以这样做
var MyVarName = ...;
函数doSomething(strVarName)
{
... doSomething._global [strVarName] ...
}
doSomething._global = this;
doSomething(''myVarName'');
PointedEars
No, don''t. `window'' may be a reference to the Global Object in many
cases, but not in all. Use a reference to the Global Object instead:
var MyVarName = ...;
var _global = this;
function doSomething(strVarName)
{
... _global[strVarName] ...
}
doSomething(''myVarName'');
If you are concerned that the new global `_global'' spoils your global
namespace, you can instead also do
var MyVarName = ...;
function doSomething(strVarName)
{
... doSomething._global[strVarName] ...
}
doSomething._global = this;
doSomething(''myVarName'');
PointedEars
window [strVarName] =某个值;
window[strVarName]=some value;
不,不要'' T。 'window''可能是许多
案例中对全局对象的引用,但并非全部。改为使用对全局对象的引用:
var myVarName = ...;
var _global = this;
函数doSomething(strVarName)
{
... _global [strVarName] ...
}
doSomething(''myVarName'');
如果你担心新的全球`_global'会破坏你的全球
命名空间,你也可以这样做
var myVarName = ...;
函数doSomething(strVarName)
{
... doSomething._global [strVarName] ...
}
doSomething._global = this;
doSomething(''myVarName'');
PointedEars
No, don''t. `window'' may be a reference to the Global Object in many
cases, but not in all. Use a reference to the Global Object instead:
var myVarName = ...;
var _global = this;
function doSomething(strVarName)
{
... _global[strVarName] ...
}
doSomething(''myVarName'');
If you are concerned that the new global `_global'' spoils your global
namespace, you can instead also do
var myVarName = ...;
function doSomething(strVarName)
{
... doSomething._global[strVarName] ...
}
doSomething._global = this;
doSomething(''myVarName'');
PointedEars
这篇关于动态变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!