Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何自动将对象生成数组_Javascript_Arrays_Object - Fatal编程技术网

Javascript 如何自动将对象生成数组

Javascript 如何自动将对象生成数组,javascript,arrays,object,Javascript,Arrays,Object,大家好,我现在正在制作这样的阵列: var piece1 = new DialoguePiece(null, questions[0], 0, 0, 4, 1); var piece2 = new DialoguePiece(null, questions[1], 1, 0, 2, 3); var piece3 = new DialoguePiece(scripts[1], null, 2, 1, 2, 2); var piece4 = new DialoguePiece(scripts[2],

大家好,我现在正在制作这样的阵列:

var piece1 = new DialoguePiece(null, questions[0], 0, 0, 4, 1);
var piece2 = new DialoguePiece(null, questions[1], 1, 0, 2, 3);
var piece3 = new DialoguePiece(scripts[1], null, 2, 1, 2, 2);
var piece4 = new DialoguePiece(scripts[2], null, 3, 1, 3, 3);
var piece5 = new DialoguePiece(scripts[0], null, 4, 0, 5, null);
var piece6 = new DialoguePiece(scripts[3], questions[2], 5, 4, 6, null);
var piece7 = new DialoguePiece(scripts[4], null, 6, 5, 7, null);
var piece8 = new DialoguePiece(scripts[5], null, 7, 6, null, null);

var pieces = [piece1, 
              piece2,
              piece3,
              piece4,
              piece5,
              piece6,
              piece7,
              piece8];
但我想知道有没有更简单的方法?例如:

var pieces = GetObjectsByName(DialoguePiece);
谢谢

function DialoguePiece(){
  DialoguePiece.all.push(this);
  ...
} 
DialoguePiece.all=[];
您可以简单地将它们存储在一个数组中,每次调用构造函数时(作为构造函数本身的属性使其易于引用)。要访问,您可以执行以下操作:

for(var i=0;i++<10;)
  new DialoguePiece();

console.log(DialoguePiece.all);
//its a normal array, so you could also do:
console.log(DialoguePiece.all.map(dia=>dia.name||"doesnt exist"));
for(变量i=0;i++直径名称| |“不存在”);

谁来决定
对话框片段
的参数是什么?我现在是我,如果编写
var-piece1=
只需像
片段那样直接将元素推到数组中。push(new DialoguePiece())
可能会将
var-piece1=…
替换为
片段。push(…)
?那么我看不到实现自动化的方法。向构造函数传递参数的方式没有明显的模式。虽然这确实解决了问题,但在我看来这是非常不自然的。它鼓励全局状态(从OOPOV技术上说是“静态的”),这可能会在以后给OP带来一些坏习惯。@byxor更好的方法?我宁愿把这个逻辑放到工厂函数中,而不是构造函数本身。所以我至少可以有多个互不干扰的工厂。其中每一个都可以有自己不同的状态。根据
DialoguePieces
构造函数的可见程度,访问
DialoguePieces的任何两个或更多代码段。所有的
都将静态地绑定在一起(JS对此更宽容一点除外)。这有点像一些人认为单身模式是一种反模式。这是有争议的。@Jonasw,这将是几乎相同的代码,但有一个重要的区别:可用性、可伸缩性和不易出错,因为每个工厂都可以与其他工厂不同。