这是代码的基本模板:
// Create your class like this
function SaveData(x1, x2, x3, x4) {
this.string1 = x1; // Whatever value you need
this.string2 = x2; // Whatever value you need
this.string3 = x3; // Whatever value you need
this.string4 = x4; // Whatever value you need
}
// Now let us construct objects from SaveData class and insert them in array.
var loadEntries = [];
var firstInstance = new SaveData("a1", "a2", "a3", "a4");
loadEntries.push(firstInstance);
// we can directly push more objects without creating variables like this:
loadEntries.push(new SaveData("b1", "b2", "b3", "b4"));
loadEntries.push(new SaveData("c1", "c2", "c3", "c4"));
loadEntries.push(new SaveData("d1", "d2", "d3", "d4"));
// Access like this in update function
var myGlobalVar = "d1";
for (var i = 0; i < loadEntries.length; i++) {
var saveDataInstance = loadEntries[i];
// let us log the instance on console to see it is correct
console.log(JSON.stringify(saveDataInstance));
if (saveDataInstance.string1 === myGlobalVar) {
console.log("Found the instance we were looking for");
// Other strings are
// saveDataInstance.string2
// saveDataInstance.string3
// saveDataInstance.string4
}
}
在这种情况下,我认为不需要关闭。
在此处查看工作演示:http: //jsbin.com/utezuq/1/watch