자바스크립트에서 변수명을 일정하게 생성하기 위해서 eval을 열심히 사용했지만 선언되지 않은 변수라고 에러가 팍팍팍팍!!!
결국 구글링신의 힘을 빌려 알아낸 결과. window[] 라는 것을 알게 되었다.
역시 구글!!!!
Today I was looking for a decent way to have variable variables in Javascript. Because I work with a whole bunch of particularly bright people*, the answer to any question is always near. I figured I'd share today's little insight! First let's look about how we'd do this in PHP:
Suppose you have a variable named $i containing a numeric value. In this example it's 1 but it could be anything. You want to create a new variable named name1. In PHP you'd use the double $ way:
$i=1;
${'name'.$i} = 'Marco';
echo "got ".$name1;
When doing the same thing in Javascript, the dreaded eval() comes to mind:
var i=1;
eval('name' + i + ' = "Marco"');
document.write('got ' + name1);
Nasty. We don't want to use eval() if we can avoid it. And most of the time we actually can! Here's a much more proper way of doing it, using the fact that all global variables are held in the window array.
var i=1;
window['name' + i] = 'Marco';
document.write('got ' + name1);
There we go! Nice, clean and no eval() necessary.
* It's absolutely delightful to work with people who all excel in what they do without the often associated 'arrogant prick attitude'. I love my job! =)
출처 : http://www.i-marco.nl/weblog/archive/2007/06/14/variable_variables_in_javascri