大家好,我现在正在制作这样的阵列:
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);
感谢。
答案 0 :(得分:1)
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"));