In javascript, local variables declared with var
have function scope. That means there is exactly one copy of that local variable per function. So, in your second code block, there is only one copy of the variable e1
and the for
loop is assigning different values to it in every iteration.
You can do what you are doing, but e1
will only have one value in it when you're done and that value will be whatever the last iteration of the for
loop assigned to it.
I don't know exactly what you're trying to accomplish, but you could create an array of your objects like this:
var erd = Joint.dia.erd;
Joint.paper("world", 800, 250);
var x = 0, y = 0;
var objs = [];
for (var i = 0; i < numOfUserDefObjects; i++) {
objs.push(erd.Entity.create({
rect: {
x: x,
y: y,
width: 100,
height: 60
},
label: "Entity"
}));
x += 20;
y -= 40;
}
// objs now contains an array of the created objects
Note: I also initialized x and y, fixed the declarations to use var
instead of int
and correctly increment and decrement x and y in the for
loop. You had a lot of syntax errors in the code you posted.