Please turn on JavaScript. To find out how to do this visit the WebWise JavaScript guide.


JavaScript defines objects and arrays as 'reference types', different to variables which are defined as 'primitive types'. Numbers and boolean variables are primitive because they consist of only their value - 123, true/false etc. Objects and arrays however, are made up of a number of properties and elements so consist of many 'primitive types'.

A primitive type can be copied very simply like so:


var foo = 1;
var bar = foo;
alert(bar); // returns 1;

Copying objects and arrays are not as easy however, because instead of making a copy of the original object, you make a reference to the original object. This is because the original object is itself a reference. Copying a reference always makes you a reference. For example:


var christmas  = new Date(2009, 12, 25);
var boxing_day = christmas;
christmas.getDate(); // returns 24

If you compare the two variables above, you will find that they are equal:


alert(christmas == boxing_day); // returns true

This is because they both refer to the same object, even though we were trying to make them refer to different dates.

Now, if you wanted to make a copy of the object 'christmas' but do not want a reference to it, you can use glow.lang.clone like so:


var christmas  = new Date(2009, 12, 25);
var boxing_day = glow.lang.clone(christmas);
alert(christmas == boxing_day); // returns false

BBC © 2014 The BBC is not responsible for the content of external sites. Read more.

This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.