我可以在 JavaScript 中创建动态对象名称吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12826154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:35:16  来源:igfitidea点击:

Can I create dynamic object names in JavaScript?

javascriptdynamic-variables

提问by Terry Carnes

Possible Duplicate:
javascript - dynamic variables
Dynamic Javascript variable names

可能重复:
javascript - 动态变量
动态 Javascript 变量名

I need to create a number of objects on a page and want to name them sequentially. Is there a way to do this in JavaScript?

我需要在页面上创建多个对象并希望按顺序命名它们。有没有办法在 JavaScript 中做到这一点?

for (i=0;i<num;i++){
  var obj+i = new myObject("param1","param2");
  obj+i.someProperty = value;
}

This way I can dynamically create a varying number of objects (dependent on the value "num") and then set their properties appropriately.

通过这种方式,我可以动态创建不同数量的对象(取决于值“num”),然后适当地设置它们的属性。

I can do this in PHP, is there a way to do it in JavaScript?

我可以在 PHP 中做到这一点,有没有办法在 JavaScript 中做到这一点?

回答by Trevor Dixon

This isn't recommended, but does what you're trying to do (if you're running in a browser and not some other js environment).

不推荐这样做,但可以执行您要执行的操作(如果您在浏览器中运行而不是在其他一些 js 环境中运行)。

for (i = 0; i < num; i++) {
  window['obj' + i] = new myObject("param1","param2");
  window['obj' + i].someProperty = value;
}
obj0.someProperty;

This works because global variables are actually properties of the window object (if you're running in the browser). You can access properties of an object using either dot notation (myObject.prop) or bracket notation (myObject['prop']). By assigning window['obj' + i], you're creating a global variable named 'obj' + i.

这是有效的,因为全局变量实际上是 window 对象的属性(如果您在浏览器中运行)。您可以使用点表示法 (myObject.prop) 或括号表示法 (myObject['prop']) 访问对象的属性。通过分配 window['obj' + i],您正在创建一个名为 'obj' + i 的全局变量。

The better option is to use an array or parent object to store your objects.

更好的选择是使用数组或父对象来存储您的对象。

myObjs = {};
for (i = 0; i < num; i++) {
  myObjs['obj' + i] = new myObject("param1","param2");
  myObjs['obj' + i].someProperty = value;
}
myObjs.obj0.someProperty;

Or use an array like lots of other answers suggest.

或者使用许多其他答案建议的数组。

回答by Felix Kling

That's what arrays are for, to hold a collection of something:

这就是数组的用途,用于保存一些东西的集合:

var objs = [];
for (i=0;i<num;i++){
  objs[i] = new myObject("param1","param2");
  objs[i].someProperty = value;
}

Dynamic variables are almost always a bad idea.

动态变量几乎总是一个坏主意。

回答by Anoop

You can create, and you can set/modify properties of that object.

您可以创建,也可以设置/修改该对象的属性。

Modified code:

修改后的代码:

var obj = {}; //
for (i=0;i<num;i++){
  obj[i] = new myObject("param1","param2");
  obj[i].someProperty = value;
}

I recommend you to use array. as

我建议你使用数组。作为

 var obj = []; //
    for (i=0;i<num;i++){
      obj[i] = new myObject("param1","param2");
      obj[i].someProperty = value;
    }