views:

150

answers:

4

Is the following code valid?

var i;
var objs={};
for (i=0; i <10; i++)
{
   objs.i=new FooObject();
}

alert(objs.4.someMethod());

If not, how should it be rewritten to accomplish what I want?

+2  A: 
var i; 
var objs = new Array();

for(i = 0; i < 10; i++)
{
   objs.push(new FooObject());
}


objs[4].someMethod();
Kevin
I don't want to use push because the numbers may not be in perfect ascending order. After 0-4, 5 and 6 may be missing and the next number may be 7. Can you rewrite it according to that?
Click Upvote
Can you provide a little more detail, I'm confused.
Kevin
+3  A: 

You cannot use numericals for variable names 1. If you want to reference an item by a numerical value, use an array 2. You can then access items by their key in the array. If you want to cycle through, you can use the for...in option 3. It won't matter if your keys are sequential and contiguous:

var x;
var myItems = new Array();
myItems[0] = "Foo";
myItems[9] = "Bar";
myItems[5] = "Fiz";

for (x in myItems) {
  alert(myItems[x]);
}

1 http://www.w3schools.com/js/js_variables.asp
2 http://www.w3schools.com/js/js_obj_array.asp
3 http://www.w3schools.com/js/tryit.asp?filename=tryjs_array_for_in

Jonathan Sampson
He's not using them for variable names but for property names; this is perfectly valid but they must be referenced using the syntax "objs[4].someMethod()" - although he wouldn't then have useful things like a magic length property and the various array instance methods, which means that he's better off using an array as you say :-)
NickFitz
I can't use arrays because while I'm using numericals, they won't be in perfect order, they may start with 10, then 5 chunks may be missing and next element may be 15. Arrays wont work like that.
Click Upvote
@Click That doesn't matter.
Jonathan Sampson
Really? I can have irregularly named array indexes? I think javascript didn't use to like those
Click Upvote
@Click Upvote - Sure. I do in my example above.
Jonathan Sampson
@Click Upvote - You will only run itno a problem if you try to guess the key values, like in an for(i = 0; i < myarray.length; i++) alert(myarray[i]); In this case you may request an index that doesn't exist.
Jonathan Sampson
What if the array starts with a value greater than 0? Will it still work?
Click Upvote
Yes .
Jonathan Sampson
+2  A: 

You should edit your code as following:

var i;
var objs = {};
for (i = 0; i < 10; i++) {
  objs[i] = new FooObject();
}

alert(objs[4].someMethod());
Sinan Taifour
If the indexes are close to each other, and you never plan to use anything other than numerical values as keys, you could replace `var objs = {};` with `var objs = [];`. It would be cleaner.
Sinan Taifour
They won't be close to each other, the for loop i used in my question was just to get the point across. The index can be irregular and random..
Click Upvote
+1  A: 

You can't use numbers as variable names, because straight up numbers exist as their own object set in Javascript (i.e, you could think of 4 as already being a global variable that you can't override).

Ryan McGrath