动态变量名称 [英] Dynamic variable names

查看:84
本文介绍了动态变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这可能是一个全新的问题,但我很难过。


如果我有这样的功能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

推荐答案

Russ Chinoy写道:
Russ Chinoy wrote:
function DoSomething(strVarName){
....
}

我称之为:

DoSomething(''myVarName'');

里面该函数,如何为名为myVarName的变量赋值(即名称为strVarName的值的变量)?
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)?



window [strVarName] =某个值;


Mi ck


window[strVarName]=some value;

Mick


Mick White写道:
Mick White wrote:
Russ Chinoy写道:
Russ Chinoy wrote:
function DoSomething(strVarName){
....
}

我称之为:

DoSomething(''myVarName'');

在函数内部,我如何为名为
myVarName的变量赋值(即名称为strVarName的值的变量)?
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)?



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


Mick White写道:
Mick White wrote:
Russ Chinoy写道:
Russ Chinoy wrote:
函数DoSomething(strVarName){
....
}
我称之为所以:

DoSomething(''myVarName'');

在函数内部,如何为名为
myVarName的变量赋值(即名称为strVarName的值的变量)?
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)?



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


这篇关于动态变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆