Javascript: variable in outer function not changed by inner function
- by Weng-Lock Mok
I am having a small issue with what I believe is probably my misunderstanding of Javascript closures.
I have this piece of code --
getStdOpts: function(tbl, filt) {
var vals = new Array();
this.srvs.getStdOptions(
{ tbl: tbl },
{
'ok': function(rsp) {
for (var i in rsp) {
vals.push({ value: rsp[i].id, text: rsp[i].descr });
}
}
}
);
return vals;
}
In essence, although the inner function inside the getStdOptions call ('ok': function...) pushes new values into the vals array, when accessed from outside the call, the vals array is empty. When accessed from within the inner function, vals contains all the elements as expected.
Would really appreciate any help I can get on this matter.