Javascript: variable in outer function not changed by inner function
Posted
by
Weng-Lock Mok
on Stack Overflow
See other posts from Stack Overflow
or by Weng-Lock Mok
Published on 2012-11-30T04:46:54Z
Indexed on
2012/11/30
5:03 UTC
Read the original article
Hit count: 387
JavaScript
|closures
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.
© Stack Overflow or respective owner