javascript - Manufacturing variable names -
i come across situations in programming want have bunch of variables defined in loop (e.g., soldiera, soldierb, soldierc,...) , assign them objects.
someclassa = ext.extend(someclassb) { initcomponent { this.weekdays = [ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday','sunday' ]; for(var = 0; i<7; i++) { var dummy = "this.vacation" +this.weekdays[i]; dummy = 1; }; console.log("i desire following 1: " +this.vacationmonday); } }
console lists undefined elements.
what recommended course of action?
you have use bracket notation set dynamic variable names. note have assign directly, not way have listed.
this["vacation" + this.weekdays[i]] = 1;
Comments
Post a Comment