Quoting through all instances of javascript object

if I have an constructor for an object, for example:

function cat(color, sex){
     this.color = color;
     this.sex = sex;
}

and I make some cats:

var fluffball = new cat("blue","male");
var shiznitz = new cat("red","male");
var slothersburger = new cat("green","female");

Can I skip all the cats that I announced? Sort of:

var current_cat;
for(current_cat in document.cat){
     alert(current_cat.color);
}

This does not work. Do people usually store all cat objects in an array? Or create another object containing an array of individual cats:

function all_cats(){
     this.the_cats = new Array();
}

Thanks for any tips!

+5
source share
5 answers

It is impossible to view all the objects you created if you do not track them anywhere (as in the constructor). Something like that -

var globalCatArray = [];

function cat(color, sex){
    this.color = color;
    this.sex = sex;
    globalCatArray.push(this);
}

var fluffball = new cat("blue","male");
var shiznitz = new cat("red","male");
var slothersburger = new cat("green","female");

//use globalCatArray to get all instances

. , . , , , .

, for..in . Javascript Array

+5

CatFactory, Cat:

:

CatFactory.createCat('fluffball', 'blue','male');
CatFactory.createCat('shiznitz', 'red','male');
CatFactory.createCat('slothersburger', 'green','female');


CatFactory.forEachCat (function () { // forEach abstraction
  alert(this.name + ' is ' + this.color);
});

:

function Cat (name, color, sex){
  this.name = name;
  this.color = color;
  this.sex = sex;
}

CatFactory = {
  createCat: function () {
    var newCat = {};
    Cat.apply(newCat, arguments);
    this.allCats.push(newCat); 
    return newCat;
  },

  allCats: [],

  forEachCat: function (action) {
    for (var i = 0; i < this.allCats.length; i++){
      action.call(this.allCats[i]);
    }
  } 
};
+4

:

var Cat = (function cat(color, sex) {
    var allCats = [],
        catConstructor = function () {
            allCats.push(this);
            this.color = color;
            this.sex = sex;
        };
    catConstructor.each = function (fn) {
        for (var i = 0; i < allCats.length; i++) {
            fn.call(allCats[i]);
        }
    };
    return catConstructor;
}()); // execute the function immediately

, , Cat.

var fluffy = new Cat('brown', 'male'),
    kitty = new Cat('black', 'female');
Cat.each(function () {
    alert(this.color);
});

, ( getAllCats(), - ).

+1
source

If you want to go through all of them, storing them in an array, this will make sense.

Something like var cats = [];

cats[0] = new cat();

cats[0].color = "red";
cats[0].name = "fluffy";

for ( var cur in cats )
{
    //Do Things
} 

Sorry for all the corrections that fell asleep today.

0
source

since I had a similar problem, here is one simple solution if you use jquery:

function Cat(color, sex){
     this.color = color;
     this.sex = sex;
}

var cats = [];
function createCat(color, sex)
{
    cats.push(new Cat(color, sex)));
}

createCat("white", "male");
createCat("black", "female");

//iterating cats by using jQuery $.each
$.each(cats, function(index, object){
        alert(object.color);
});
0
source

All Articles