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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -