I’ve got a set up that, in stylised form, is like this:
(function () {
var db = {
com: { EDIT: Changed this from [...] to {...}
324: {
unmod: "xyz"
}
}
};
var report = {
mymethod: function () {
var x = db.com[324].unmod;
}
};
})();
The error message I’m getting is db.com is undefined. It seems to me that report.mymethod cannot “see” db.com. Would that be right?
One factor might be that db.com is created by looping through AJAX data like this:
for (i = 0; i < length1; i++) {
cat = o.cat[i];
length2 = cat.com.length;
// Loop through comments
for (j = 0; j < length2; j++) {
com = cat.com[j];
// Create db object
this[com.cnum] = {
unmod: com.unmod
};
}
}
Is the issue one of closure?
The relevant part of the AJAX data is like this
"cat":[
{ ...
"com": [ {"cnum":"324", "unmod":"xyz"},...]
}, ...],
Apologies that the AJAX names and property names are the same. It makes it easier for me, but perhaps not for y’all 🙂
Example code works as expected after fixing the population part: http://jsfiddle.net/4bL8T/