문제

I am loading multiple Google maps in JavaScript, but the only way I was able to accomplish this in JavaScript was to use "eval".

I've read that eval is insecure. Can the following line be simplified or done another way?

eval('var map' + uin + ' = ' + 'new google.maps.Map(map_canvas, map_options);');
도움이 되었습니까?

해결책

var maps = {};
maps[uin] = new google.maps.Map(map_canvas, map_options)

Using objects is fun!

다른 팁

There's no need to create a dynamic variable name.

How about creating an array of maps?

var maps  = [];

maps[ 0 ] = new google.maps.Map(map_canvas, map_options);

maps[ 1 ] = new google.maps.Map(map_canvas, map_options);

But it looks like you're using the same map_canvas for all these maps – that's probably going to screw something up.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top