views:

79

answers:

2

I am unsure how javascript stores objects, and references them. So, I don't know if my plan will cause bad performance. Any insight would be appreciated.

I have many divs on a website, and each has a unique id. I have an object constructor:

function box_object(box_id){
     this.the_box = document.getElementById(box_id);
     this.related_boxes = new Array();
}

Each box object contains a property 'the_box', which is a div on the page. Also, each box has an array called related_boxes which would be filled with other box_objects.

The reason I am concerned is because if there are 50 divs on the page, each one could have 10 related boxes. I think / hope that javascript is just storing objects in the array 'by reference' and the memory usage is small. The code obviously does more, but I tried to condense it into this example for a short question.

Does anyone have experience doing something with a lot of objects like this?

Thanks

+3  A: 

While every implementation of Javascript can be (and is!) utterly different from all others, I believe they all do the "storing by reference" that you're hoping for.

Alex Martelli
A: 

Yes, they are stored by reference, but there are nuances to understand. If you have the time, I highly recommend watching this video.

Peter Bailey