I have a Javascript function something like this, defined in a place I can't change it:
foo.customSave = [];
foo.save = function() {
var saveString = 'initial value';
// stuff about saveString here
for (i in foo.customSave) { foo.customSave[i](); }
// more stuff about saveString here, including actually saving it
};
Then I have my own code like this:
bar.init = function() {
// other init stuff here
foo.customSave.push(function() {
saveString += 'something meaningful here';
});
// more other init stuff here
};
bar.init() is called at an appropriate time (which is to say, before foo.save() is called). The problem seems to be that saveString is not defined when I try to add 'something meaningful here' to it (putting a console.log call there confirms this).
Is there any way my customSave function can access that string, or am I stuck?
saveStringis only accessible in functionfoo.save, you cannot access it from elsewherefooyou have zero chance of accessing saveString outside offoo.save