There are several issues here.
- Storing several instances of object(s) in memory
This is generally done using an array or some more complex data structure. You can use a single array to hold everything (with polymorphism, for instance) or have one array per type of objects. Which is preferable depends on the kind of behaviour your objects will have.
Objects don't need to be stored in a variable at all, they can be an array member. You can do
a = new Array();
a.push(new MyGameObject(some params ... ));
Alternatively (more typically), you might temporarily assign the object to a local variable in the function, then set some properties, or call initialisation methods
function init_everything() // Call this when your game starts, or when a new level starts etc.
{
objects_list = new Array();
}
function add_a_new_thingy() {
var t = new Thingy();
t.someproperty = 42;
t.init_method();
objects_list.push(t);
}
Then call add_a_new_thingy several times.
- Initialising some objects in some pattern at the start of a game or level
At the start of the game, or at the beginning of a level, you'll probably want to set up some objects with known states, in your global array of things (or arrays of things, if you have more than one).
Simplest way: hard-code each level as a Javascript function somewhere, which creates them all (NB: You don't need to put it in the same .js file as other things!)
Slightly more complex way: Create some kind of JS data structure, which you read to create the objects:
var level1_things = [
{ type:"monster", x:42, y:99 },
{ type:"bonus", x:128, y:100 }
];
Even more complex way: read them in some other format from the server, using XMLHttpRequest.
You could, for example, parse a text-file where certain characters indicate types of objects, so if you did a Sodoku-type game, you have a "W" for "wall", "P" for "player", "B" for box, "T" for target or something (maybe a blank space for an empty tile).