Results 1 to 5 of 5

Thread: Dynamic naming of vars in for loop

  1. #1
    Join Date
    Jan 2007
    Posts
    51
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Dynamic naming of vars in for loop

    I'm trying to create a variable per 'for' loop instance.

    I get a 'missing var name' at the line in question in firebug

    Code:
    var sArray = new Array(1,2,3,4,5,6,7);
    	
     	function addSMarkers(sArray)
    	{
    		for(var n=0; n < sArray.length; n++)
    		{
    			// create cloned icon object for each marker
    			// This is where I don't know how to code
    			// to create a new var for each item in the array
    			var ['cloneIcon'+n] = 'clone'+n;
    			//alert each one created when we learn how to declare them 		
    			alert('eachNewVar'); 
    		}
    	}
     
    	 alert('test');
    	 alert(sArray.length);
    	 addSMarkers(sArray);
    Thoughts?

    Thanks for your time.
    BN
    Last edited by jscheuer1; 08-01-2008 at 01:27 AM. Reason: format code

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This:

    Code:
    var ['cloneIcon'+n] = whatever
    looks very suspicious to me. I can't recall anything like that ever working. You probably need to use eval there, or better yet, make it a property of an existing object, like:

    Code:
    window['cloneIcon'+n] = whatever
    But then of course, anywhere else you wanted to access it or check its value, you would need to refer to it as a property of the window object.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    ['foo'] means 'a new array containing the string "foo"'. If you're trying to use variable variables, you're almost certainly doing something wrong. Instead, I suggest creating an object and assigning these values as properties of it using the square-bracket syntax as John said (but not on the global 'window' object).

    As it stands, the only part that's different is the numerical part, so you could as well use an array:
    Code:
    function map(f, a) {
      for (var i = 0, n = a.length, r = []; i < n; r[i] = f(a[i], i++));
      return r;
    }
    
    function flip(f) {
      return function(a, b) {
        return f(b, a);
      };
    }
    
    function compose(fa, fb) {
      return function() {
        fb(fa.apply(this, Array.prototype.slice.call(arguments)));
      };
    }
    
    function f_prepend(a) {
      return function(b) {
        return a + b;
      };
    }
    
    function makeSMarkers(sArray) {
      return map(flip(f_prepend('clone')), sArray);
    }
    
    function addSMarkers(sArray) {
      cloneIcon = makeSMarkers(sArray);
    }
    Last edited by Twey; 08-01-2008 at 09:18 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm with you Twey, I generally like using an object in scope, or at least a unique one. I was just trying to introduce the concept, and window seemed very handy for that. The only problems with using the window or other native objects I'm aware of are that one might overwrite an existing property or end up trying to overwrite a readonly property. Since these properties can and do vary by browser, it is virtually impossible to keep track of them all, and new ones may be introduced with little or no notice on the part of browser manufacturers.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2008
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I'm not sure I understand Twey's explanation, so I'll put it in words that I think are easier understood.

    Since your created variables are to be of fixed quantity (sArray.length), of fixed use (just "clone" + n), and limited in scope (exists only in the function), it would be very easy to simply use a second, local array for this purpose.

    sArray should also be defined with brackets, not parentheses.

    Code:
    <head>
    <script>
    
    var sArray = new Array[1,2,3,4,5,6,7];
    	
     	function addSMarkers(theArray)
    	{
    	var tempArray = new Array(theArray.length);
    		for(var n=0; n < theArray.length; n++)
    		{
    			tempArray [n] = 'clone'+n;
    			alert(tempArray[0]); 
    		}
    	}
    </script>
    </head>
     
    <body>
    <script>
    	 alert('test');
    	 alert(sArray.length);
    	 addSMarkers(sArray);
    </script>
    </body>
    Last edited by newshound; 08-01-2008 at 10:52 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •