javascript - For loop creating infinite object tree -
i have 2 loops creating object:
function newimage(){ image = {}; var temp = {} for(i=0;i!=250;i++){ temp[i] = {}; } image = temp; for(i=0;i!=250;i++){ image[i] = temp; } }
this should create object 250 values, each being object contains 250 objects. however, creates object creates 250 values, fills 250 values, , loops while. haven't found end of tree, doesn't freeze leading me believe finite. i've checked iterations 50 , works way (it doesn't make long tree). seems if happening during last iterations. here's full thing.
var temp = {} for(i=0;i!=250;i++){ temp[i] = {}; }
the lines above create object , populate 250 other objects (so far good).
then image = temp;
sets image (global) variable temp
(so image contains 250 objects) then:
for(i=0;i!=250;i++){ image[i] = temp; }
this replaces each of 250 objects (overwriting previous assignments) reference parent object object has 250 attributes refer itself.
effectively wrote is:
image = {}; ( var = 0; < 250; i++ ) image[i] = image;
it appears have infinite tree of objects whereas have single object has many attributes refer whenever descend child end @ parent (and expanding object hierarchy in browser makes appear infinite tree when showing same object on , over).
what meant write is:
function newimage(){ var temp = {}; for( var i=0; < 250; i++){ temp[i] = {}; for( var j=0; j<250; j++){ temp[i][j] = {}; } } return temp; }
Comments
Post a Comment