javascript - Create several instances of a module with different values -


this simple problem need create javascript equivalent n instances of 'class' state must totally separate.

like:

var car = new car('ford'); var car = new car('toyota'); 

how can achieve this?

you can use array object store them:

var cars = []; cars.push(new car('ford')); cars.push(new car('toyota')); cars[0].beep(); 

you can iterate on stored instances using for loop:

for (var = 0; < cars.length; i++) { var car = cars[i]; car.beep(); } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -