PDA

View Full Version : Are objects accessed/stored as references?


darkgaro
04-09-2006, 03:57 PM
Are objects stored as references or are they caopied when they are assigned to the varaibles.

lets say I have this code


var myArray = new Array();
for (i=1; i<=10; i++)
{
myArray[i] = new LzView(canvas, {});
}

var firstElement = myArray[1];


When I assign the myArray[1] to a new variable 'firstElement' is the LzView object referenced or copied ?

And if it is not asgined as reference , can you do it and how ?

Thanks

Shelby
04-10-2006, 06:31 AM
They are stored as references. I assume this is a JavaScript thing. To illustrate this, here is a simple app you can run.


<canvas debug="true">

<script>
var a;
var b;
</script>

<simplelayout axis="x" spacing="5"/>

<button text="Create View">
<method event="onclick">
var aView = new LzView(canvas, {id:"myView", width:100});
a = aView;
b = a;
Debug.write('View Created');
</method>
</button>

<button text="Set b Width to 50" onclick="b.setWidth(50);"/>

<button text="Set a Width to 150" onclick="a.setWidth(150);"/>

<button text="report widths">
<method event="onclick">
Debug.write('a.width = ', a.width);
Debug.write('b.width = ', b.width);
</method>
</button>

</canvas>



I believe that LzDataElement has a function to duplicate the node, but I haven't searched to see if LzNode's in general have such a function.