如何为变量名指定索引?例如,project0,project1,project2等?
for (var i=0; i<data.length; i++)
{
var project+i = new GanttProjectInfo(1, "Applet redesign", new Date(2010, 5, 11));
var parentTask+i = new GanttTaskInfo(1, "Old code review", new Date(2010, 5, 11), 208, 50, "");
project1.addTask(parentTask+i);
// Load data structure
ganttChartControl.addProject(project+i);
// Build control on the page
}
答案 0 :(得分:2)
正如@JaredFarrish建议的那样,您只需使用数组来存储所有项目。在变量被覆盖之前创建它们并将它们添加到数组中:
var projects = [];
for (var i = 0; i < data.length; i++) {
var project = new GanttProjectInfo(1, "Applet redesign", new Date(2010, 5, 11));
var parentTask = new GanttTaskInfo(1, "Old code review", new Date(2010, 5, 11), 208, 50, "");
project.addTask(parentTask);
ganttChartControl.addProject(project);
// Push this project to the array to save it
projects.push(project);
}
// Need to access the first project?
projects[0].foo();
答案 1 :(得分:0)
使用eval()
eval((“varname”+ i)+“=”+ 5 + i);
但实际上并不建议使用eval,总是有办法解决你想要做的事情。