在JavaScript中通过名称字符串动态获取全局变量

时间:2022-05-20 23:38:28
<script>
//in one script
var someVarName_10 = 20;
</script>

I want to get access to this variable from another script by name of variable. With window object its simple, is it possible with local variable?

我想通过变量名从另一个脚本获得对这个变量的访问。对于窗口对象,是否可以使用局部变量?

I mean access this var by code like this:

我的意思是通过如下代码访问var:

<script>
  alert(all_vars['someVar' + 'Name' + num]);
</script>

5 个解决方案

#1


85  

Do you want to do something like this?

你想做这样的事吗?

<script>
//in one script
var someVarName_10 = 20;

alert(window["someVarName_10"]); //alert 20

</script>

Update: because OP edited the question.

更新:因为OP编辑了问题。

<script>
  num=10;
  alert(window['someVar' + 'Name_' + num]); //alert 20
</script>

#2


32  

I've noticed that everyone is advising global var creation this will lead to variables leak to global namespace. When you dynamically creating classnames or just variables it's easy to keep em local:

我注意到,每个人都建议创建全局变量var,这将导致变量泄漏到全局名称空间。当你动态地创建类名或只是变量时,很容易将它们放在本地:

this['className'] = 123;

or

this['varName'] = 123;

Name-spacing would look like this:

名称间距是这样的:

vars = {};
vars['varName'] = 123;
vars.varName // 123

#3


9  

<script>
    var someVarName_10 = 20;
    var num = 10;
    alert(eval('someVar' + 'Name_' + num)); //alert 20
</script>

#4


1  

well, for debugging purpose only, you could do something like this. I use it during the development of classes, where some variables must remain private (var). this work even in local variable (and global of curse)

为了调试目的,你可以这样做。我在开发类时使用它,其中一些变量必须保持私有(var)。这项工作即使是局部变量(和全局变量)

function MYCLASS(){
    var a=1, b=2, c=3;
    this.public = "variable";
    this.debug = function(sVar){
        return eval(sVar);
    }
}

var myThing = new MYCLASS();
myThing.debug('a') //return 1
myThing.debug('b') //return 2
myThing.debug('c') //return 3

#5


-5  

If this is what you said:

如果你是这么说的:

<script type="text/javascript">
var hello = 'test';
</script>
<script type="text/javascript">
  alert (hello);
</script>

It works because script are finally available to the document and you can access their vars.

它可以工作,因为脚本最终可用于文档,您可以访问它们的vars。

#1


85  

Do you want to do something like this?

你想做这样的事吗?

<script>
//in one script
var someVarName_10 = 20;

alert(window["someVarName_10"]); //alert 20

</script>

Update: because OP edited the question.

更新:因为OP编辑了问题。

<script>
  num=10;
  alert(window['someVar' + 'Name_' + num]); //alert 20
</script>

#2


32  

I've noticed that everyone is advising global var creation this will lead to variables leak to global namespace. When you dynamically creating classnames or just variables it's easy to keep em local:

我注意到,每个人都建议创建全局变量var,这将导致变量泄漏到全局名称空间。当你动态地创建类名或只是变量时,很容易将它们放在本地:

this['className'] = 123;

or

this['varName'] = 123;

Name-spacing would look like this:

名称间距是这样的:

vars = {};
vars['varName'] = 123;
vars.varName // 123

#3


9  

<script>
    var someVarName_10 = 20;
    var num = 10;
    alert(eval('someVar' + 'Name_' + num)); //alert 20
</script>

#4


1  

well, for debugging purpose only, you could do something like this. I use it during the development of classes, where some variables must remain private (var). this work even in local variable (and global of curse)

为了调试目的,你可以这样做。我在开发类时使用它,其中一些变量必须保持私有(var)。这项工作即使是局部变量(和全局变量)

function MYCLASS(){
    var a=1, b=2, c=3;
    this.public = "variable";
    this.debug = function(sVar){
        return eval(sVar);
    }
}

var myThing = new MYCLASS();
myThing.debug('a') //return 1
myThing.debug('b') //return 2
myThing.debug('c') //return 3

#5


-5  

If this is what you said:

如果你是这么说的:

<script type="text/javascript">
var hello = 'test';
</script>
<script type="text/javascript">
  alert (hello);
</script>

It works because script are finally available to the document and you can access their vars.

它可以工作,因为脚本最终可用于文档,您可以访问它们的vars。