reference - De-referencing a JavaScript property from inside an array -


this question has answer here:

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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -