reference - De-referencing a JavaScript property from inside an array -
this question has answer here:
- javascript closure not working 1 answer
i'm trying de-reference property on javascript object, not getting expected result.
i have array of knockout view-models (i don't think problem knockout-specific), each of has observable, selected. add subscription observable function, crossselecttargetlangs called when value of selected changed.
furthermore, add subscription inside for... loop.
var tl = 0,     tlmax = alllangvms.length,     vmlang,     selectedcode;  // each 'vmlang' view-model in 'alllangvms' array... (; tl < tlmax; tl++) {      // local variable context view-model         vmlang = alllangvms[tl];      // add subscription observable         vmlang.selected.subscribe(function() {          // de-reference vmlang.code property         selectedcode = (function(code) {             return code;         }(vmlang.code));          // pass de-ref'd value target function             crossselecttargetlangs(selectedcode);     }); } however, regardless of view-model had selected observable updated, argument passed target function code last element in array, i.e. doesn't appear de-referencing.
what doing wrong?
the problem making dereferencing in wrong place. code should this:
var tl = 0,      tlmax = alllangvms.length,      vmlang,      selectedcode;    // each 'vmlang' view-model in 'alllangvms' array...  (; tl < tlmax; tl++) {        // local variable context view-model          vmlang = alllangvms[tl];        (function(vmlangparam) {           vmlangparam.selected.subscribe(function() {               crossselecttargetlangs(vmlangparam.code);        });      })(vmlang);        }
Comments
Post a Comment