3

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?

4 Answers 4

4

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());
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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..
2
var i; 
var objs = new Array();

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


objs[4].someMethod();

2 Comments

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?
Can you provide a little more detail, I'm confused.
2

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

6 Comments

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 :-)
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.
Really? I can have irregularly named array indexes? I think javascript didn't use to like those
@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.
What if the array starts with a value greater than 0? Will it still work?
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.