Timeline for answer to Variable name within a loop by Sirko
Current License: CC BY-SA 3.0
Post Revisions
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 30, 2013 at 15:17 | comment | added | Sirko |
@Figaro Like mentioned many times in the comments, this is no good approach in general. Just use an object or array and put all your variables in there. But yes, this[count] should work, but maybe has some side effects, you don't want (see object orientation in JS for what this[x] is usually used).
|
|
| May 30, 2013 at 15:01 | comment | added | Dimitri Vorontzov |
Tried window[count], @Sirko, it does what I want it to do, but it slows down loading of the page considerably. How would you bind variables to this - this[count], or in some other fashion?
|
|
| May 30, 2013 at 14:53 | comment | added | Sirko |
@Figaro a starting point might be the answers in this question, but I know of no equivalent of the window object for a function scope. You could maybe bind your variables to this.
|
|
| May 30, 2013 at 14:49 | comment | added | Dimitri Vorontzov | Sounds like a step in the right direction, @Sirko. That's exactly what I want to do, the variables with "dynamic names", but I think I want them local in scope – how can I do that? | |
| May 30, 2013 at 14:42 | comment | added | Sirko |
@Figaro If you want to create variables with "dynamic names" you need some pointer to the scope, where you want to add them. If this is the global scope, e.g., you could replace my_array with window inside the for loop, to create global variables of the respective name.
|
|
| May 30, 2013 at 14:40 | comment | added | Dimitri Vorontzov | That would be the obvious solution, but that's not what I want, for a number of reasons. I actually want to include the number in the name of the variablem rather than use my_array[count]. | |
| May 30, 2013 at 14:39 | history | answered | Sirko | CC BY-SA 3.0 |