PDA

View Full Version : Dynamic naming of vars in for loop



brentnicholas
07-31-2008, 09:43 PM
I'm trying to create a variable per 'for' loop instance.

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


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

jscheuer1
08-01-2008, 01:37 AM
This:



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:


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.

Twey
08-01-2008, 09:00 AM
['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:
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);
}

jscheuer1
08-01-2008, 09:31 AM
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.

newshound
08-01-2008, 10:30 AM
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.



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