Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with for loop in javascript

Member
 
Join Date: May 2008
Posts: 118
#1: Jul 16 '09
Hi,


I am developing the google application. In my google.js file consists the

Expand|Select|Wrap|Line Numbers
  1. icon1=new GIcon();
  2.     icon1.image = "images/bmw-small.png";
  3.     icon1.shadow = "images/shadow50.png";
  4.     icon1.iconSize = new GSize(25, 25);
  5.     icon1.shadowSize = new GSize(20, 19);
  6.     icon1.iconAnchor = new GPoint(10, 15);
  7.     icon1.infoWindowAnchor = new GPoint(9, 2);
  8.     icon1.infoShadowAnchor = new GPoint(18, 25);
  9.     icon2 = new GIcon();
  10.     icon2.image = "images/bmw-small.png";
  11.     icon2.shadow = "images/shadow50.png";
  12.     icon2.iconSize = new GSize(25, 25);
  13.     icon2.shadowSize = new GSize(20, 19);
  14.     icon2.iconAnchor = new GPoint(15, 20);
  15.     icon2.infoWindowAnchor = new GPoint(9, 2);
  16.     icon2.infoShadowAnchor = new GPoint(18, 25);
  17.     icon3 = new GIcon();
  18.     icon3.image = "images/bmw-small.png";
  19.     icon3.shadow = "images/shadow50.png";
  20.     icon3.iconSize = new GSize(25, 25);
  21.     icon3.shadowSize = new GSize(20, 19);
  22.     icon3.iconAnchor = new GPoint(20, 25);
  23.     icon3.infoWindowAnchor = new GPoint(9, 2);
  24.     icon3.infoShadowAnchor = new GPoint(18, 25);
  25.     icon4 = new GIcon();
  26.     icon4.image = "images/bmw-small.png";
  27.     icon4.shadow = "images/shadow50.png";
  28.     icon4.iconSize = new GSize(25, 25);
  29.     icon4.shadowSize = new GSize(20, 19);
  30.     icon4.iconAnchor = new GPoint(25, 30);
  31.     icon4.infoWindowAnchor = new GPoint(9, 2);
  32.     icon4.infoShadowAnchor = new GPoint(18, 25);
  33.     icon5 = new GIcon();
  34.     icon5.image = "images/bmw-small.png";
  35.     icon5.shadow = "images/shadow50.png";
  36.     icon5.iconSize = new GSize(25, 25);
  37.     icon5.shadowSize = new GSize(20, 19);
  38.     icon5.iconAnchor = new GPoint(30, 35);
  39.     icon5.infoWindowAnchor = new GPoint(9, 2);
  40.     icon5.infoShadowAnchor = new GPoint(18, 25);
  41.  

Now i want to replace this with for llo condition .For that purpose i have changed that as

Expand|Select|Wrap|Line Numbers
  1.  
  2. for(var i=1;i<=5;i++){
  3. icon5 = new GIcon();
  4.     'icon'+i.image = "images/bmw-small.png";
  5.     'icon'+i.shadow = "images/shadow50.png";
  6.     'icon'+i.iconSize = new GSize(25, 25);
  7.     'icon'+i.shadowSize = new GSize(20, 19);
  8.     'icon'+i.iconAnchor = new GPoint(30, 35);
  9.     'icon'+i.infoWindowAnchor = new GPoint(9, 2);
  10.     'icon'+i.infoShadowAnchor = new GPoint(18, 25);
  11. }
  12.  
But this for llop condition is not working. Please anybody tell the whats the problem in my code and how can use to work the for loop condition.Please help me Its very urgent.



Thanks
Swetha

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#2: Jul 16 '09

re: Problem with for loop in javascript


you may use a js-object to achieve that goal easyly - note the comments:

Expand|Select|Wrap|Line Numbers
  1. var icons = {};
  2.  
  3. for (var i = 1; i <= 5; i++) {
  4.     // build our icon's names
  5.     var iconName = 'icon' + i;
  6.  
  7.     // add a new instance of GIcon to our 
  8.     // 'list'-object
  9.     icons[iconName] = new GIcon() ;
  10.  
  11.     // for simpler use we store a ref to the last
  12.     // created obj
  13.     var icon = icons[iconName];
  14.  
  15.     // now we assign the properties
  16.     icon.image = "images/bmw-small.png";
  17.     icon.shadow = "images/shadow50.png";
  18.     icon.iconSize = new GSize(25, 25);
  19.     icon.shadowSize = new GSize(20, 19);
  20.     icon.iconAnchor = new GPoint(30, 35);
  21.     icon.infoWindowAnchor = new GPoint(9, 2);
  22.     icon.infoShadowAnchor = new GPoint(18, 25);
  23. }
  24.  
now refer to a specific icon with: icons.iconName or icons['iconName'].

kind regards
Newbie
 
Join Date: Jul 2009
Posts: 4
#3: Jul 16 '09

re: Problem with for loop in javascript


The way that gits suggested is definitely the best way to do it, you should not be creating globals willy nilly. But since you were trying to create global variables, I'm going to show you how to do it.

His suggestion was to create properties inside an object. Guess what? Global variables belong to the window object, so.....

Expand|Select|Wrap|Line Numbers
  1. for (var i = 1; i <= 5; i++) {
  2.     // build our icon's names
  3.     var iconName = 'icon' + i;
  4.  
  5.     window[iconName] = new GIcon() ;
  6.  
  7.     // for simpler use we store a ref to the last
  8.     // created obj
  9.     var icon= window[iconName];
  10.  
  11.     // now we assign the properties
  12.     icon.image = "images/bmw-small.png";
  13.     icon.shadow = "images/shadow50.png";
  14.     icon.iconSize = new GSize(25, 25);
  15.     icon.shadowSize = new GSize(20, 19);
  16.     icon.iconAnchor = new GPoint(30, 35);
  17.     icon.infoWindowAnchor = new GPoint(9, 2);
  18.     icon.infoShadowAnchor = new GPoint(18, 25);
  19. }
Even better however, would be to create an array and store your GIcons objects, otherwise you have to append the index to the property name which is kind of silly.

Expand|Select|Wrap|Line Numbers
  1. var icons = [];
  2.  
  3. for (var i = 1; i <= 5; i++) {
  4.  
  5.     var icon= new GIcon();
  6.  
  7.     // now we assign the properties
  8.     icon.image = "images/bmw-small.png";
  9.     icon.shadow = "images/shadow50.png";
  10.     icon.iconSize = new GSize(25, 25);
  11.     icon.shadowSize = new GSize(20, 19);
  12.     icon.iconAnchor = new GPoint(30, 35);
  13.     icon.infoWindowAnchor = new GPoint(9, 2);
  14.     icon.infoShadowAnchor = new GPoint(18, 25);
  15.     icons.push(icon);
  16. }
Now you can access your icons with

icons[0]
icons[3]
Reply