Let's say I have an example, where somebody can order a car by picking options.
If they order two cars, I want to be able to use the same function, but not "clobber" the variable name. Here is an example:
cars = [];
function car(color, kind) {
this.color = color;
this.kind = kind;
}
div.addEventListener('click', function(){
if(cars.length) {
newCar = new car('red', 'chrysler');
} else {
newCar = new car('red', 'buick');
}
cars.push(newCar)
}, false);
Am I correct that everytime the div
is clicked, a new car
is made (not that the original car is changed)?
I just don't see how I can go back and edit the first car if they have the same variable name.
It would be cool to be able to say var car+cars.length
and get cars1
, cars2
, etc.