contents   index   previous   next

Composite data types


Whereas primitive types are passed by value, composite types are passed by reference. When a composite type is assigned to a variable or passed to a parameter, only a reference that points to its data is passed. The following fragment illustrates:


var AnObj = new Object; = "Joe";

AnObj.old = ReturnName(AnObj)


function ReturnName(CurObj)





After the object AnObj is created, the string "Joe" is assigned, by value since a property is a variable within an Object, to the property Two copies of the string "Joe" exist. When AnObj is passed to the function ReturnName, it is passed by reference. CurObj does not receive a copy of the Object, but only a reference to the Object. With this reference, CurObj can access every property and method of the original. If were to be changed while the function was executing, then would be changed at the same time. When AnObj.old receives the return from the function, the return is assigned by value, and a copy of the string "Joe" transferred to the property. Thus, AnObj holds two copies of the string "Joe": one in the property .name and one in the property .old. Three total copies of "Joe" exist, counting the original string literal.


The composite data types are: Object and Array.


Object type

Array type