Problem with for loop in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swethak
    New Member
    • May 2008
    • 118

    Problem with for loop in javascript

    Hi,


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

    Code:
    icon1=new GIcon();
    	icon1.image = "images/bmw-small.png";
    	icon1.shadow = "images/shadow50.png";
    	icon1.iconSize = new GSize(25, 25);
    	icon1.shadowSize = new GSize(20, 19);
    	icon1.iconAnchor = new GPoint(10, 15);
    	icon1.infoWindowAnchor = new GPoint(9, 2);
    	icon1.infoShadowAnchor = new GPoint(18, 25);
    	icon2 = new GIcon();
    	icon2.image = "images/bmw-small.png";
    	icon2.shadow = "images/shadow50.png";
    	icon2.iconSize = new GSize(25, 25);
    	icon2.shadowSize = new GSize(20, 19);
    	icon2.iconAnchor = new GPoint(15, 20);
    	icon2.infoWindowAnchor = new GPoint(9, 2);
    	icon2.infoShadowAnchor = new GPoint(18, 25);
    	icon3 = new GIcon();
    	icon3.image = "images/bmw-small.png";
    	icon3.shadow = "images/shadow50.png";
    	icon3.iconSize = new GSize(25, 25);
    	icon3.shadowSize = new GSize(20, 19);
    	icon3.iconAnchor = new GPoint(20, 25);
    	icon3.infoWindowAnchor = new GPoint(9, 2);
    	icon3.infoShadowAnchor = new GPoint(18, 25);
    	icon4 = new GIcon();
    	icon4.image = "images/bmw-small.png";
    	icon4.shadow = "images/shadow50.png";
    	icon4.iconSize = new GSize(25, 25);
    	icon4.shadowSize = new GSize(20, 19);
    	icon4.iconAnchor = new GPoint(25, 30);
    	icon4.infoWindowAnchor = new GPoint(9, 2);
    	icon4.infoShadowAnchor = new GPoint(18, 25);
    	icon5 = new GIcon();
    	icon5.image = "images/bmw-small.png";
    	icon5.shadow = "images/shadow50.png";
    	icon5.iconSize = new GSize(25, 25);
    	icon5.shadowSize = new GSize(20, 19);
    	icon5.iconAnchor = new GPoint(30, 35);
    	icon5.infoWindowAnchor = new GPoint(9, 2);
    	icon5.infoShadowAnchor = new GPoint(18, 25);

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

    Code:
    for(var i=1;i<=5;i++){
    icon5 = new GIcon();
    	'icon'+i.image = "images/bmw-small.png";
    	'icon'+i.shadow = "images/shadow50.png";
    	'icon'+i.iconSize = new GSize(25, 25);
    	'icon'+i.shadowSize = new GSize(20, 19);
    	'icon'+i.iconAnchor = new GPoint(30, 35);
    	'icon'+i.infoWindowAnchor = new GPoint(9, 2);
    	'icon'+i.infoShadowAnchor = new GPoint(18, 25);
    }
    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.Pleas e help me Its very urgent.



    Thanks
    Swetha
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

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

    Code:
    var icons = {};
    
    for (var i = 1; i <= 5; i++) {
        // build our icon's names
        var iconName = 'icon' + i;
    
        // add a new instance of GIcon to our 
        // 'list'-object
        icons[iconName] = new GIcon() ;
    
        // for simpler use we store a ref to the last
        // created obj
        var icon = icons[iconName];
    
        // now we assign the properties
        icon.image = "images/bmw-small.png";
        icon.shadow = "images/shadow50.png";
        icon.iconSize = new GSize(25, 25);
        icon.shadowSize = new GSize(20, 19);
        icon.iconAnchor = new GPoint(30, 35);
        icon.infoWindowAnchor = new GPoint(9, 2);
        icon.infoShadowAnchor = new GPoint(18, 25);
    }
    now refer to a specific icon with: icons.iconName or icons['iconName'].

    kind regards

    Comment

    • juanmendes
      New Member
      • Jul 2009
      • 4

      #3
      Another way

      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.....

      Code:
      for (var i = 1; i <= 5; i++) {
          // build our icon's names
          var iconName = 'icon' + i;
       
          window[iconName] = new GIcon() ;
       
          // for simpler use we store a ref to the last
          // created obj
          var icon= window[iconName];
       
          // now we assign the properties
          icon.image = "images/bmw-small.png";
          icon.shadow = "images/shadow50.png";
          icon.iconSize = new GSize(25, 25);
          icon.shadowSize = new GSize(20, 19);
          icon.iconAnchor = new GPoint(30, 35);
          icon.infoWindowAnchor = new GPoint(9, 2);
          icon.infoShadowAnchor = new GPoint(18, 25);
      }
      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.

      Code:
      var icons = [];
      
      for (var i = 1; i <= 5; i++) {
       
          var icon= new GIcon();
       
          // now we assign the properties
          icon.image = "images/bmw-small.png";
          icon.shadow = "images/shadow50.png";
          icon.iconSize = new GSize(25, 25);
          icon.shadowSize = new GSize(20, 19);
          icon.iconAnchor = new GPoint(30, 35);
          icon.infoWindowAnchor = new GPoint(9, 2);
          icon.infoShadowAnchor = new GPoint(18, 25);
          icons.push(icon);
      }
      Now you can access your icons with

      icons[0]
      icons[3]

      Comment

      Working...