[thelist] JavaScript array problem

Fernando J. Gomez fgomez at criba.edu.ar
Fri Aug 8 16:40:58 CDT 2003


Hi!

I'm having a problem with the following (simplified) piece of code:


<script language="JavaScript" type="text/javascript">
<!--
var array1 = new Array();
var array2 = new Array();
array1 = array2;

//--------------------------------------------------------
function addElement()
//--------------------------------------------------------
{
  alert("array2.length=" + array2.length);
  array1[array1.length] = "xxx";
  alert("array2.length=" + array2.length);
}

//-->
</script>


Note that I'm only modifying array1, but as the alerts show, array2 is also
changing! It is as if both array1 and array2 were two names for the same
array. I hadn't noticed this kind of behavior in JS before.

How should I modify array1 without altering array2?

The real situation is this: array1 and array start being equal, but as the
user interacts with a form, he modifies array1. In case he cancels the
operation, the values in array2 are restored.

You can test the above code here:

  http://recinfo.uns.edu.ar/opacmarc/test-ejemplares.htm


Thanks in advance for your ideas. And please send a copy of your replies to
my address, since I'm on digest mode.

Best wishes,

Fernando



=======================================================
Fernando J. Gómez

Biblioteca Dr. Antonio Monteiro
Instituto de Matemática de Bahía Blanca (CONICET/UNS)
Av. Alem 1253
8000 Bahía Blanca - Argentina
=======================================================




More information about the thelist mailing list