Looping through all instances of a javascript object

后端 未结 5 2023
清酒与你
清酒与你 2020-12-10 07:01

if I have an object constructor like:

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

and I make some cats:

5条回答
  •  青春惊慌失措
    2020-12-10 08:08

    If you want to go through all of them storing them in array would make sense..

    Something along the lines of 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 edits- half asleep tonight.

提交回复
热议问题