* You are viewing Posts Tagged ‘cloning’

Properly Cloning Arrays in JavaScript

This one came up during a discussion about Prototype 1.5.1 while looking through its documentation. Basically when you do the following in JavaScript, you are not copying the array, but rather creating another reference to it. In order to properly clone or duplicate an array you have two choices; loop through the array and copying its values or using the concat method. I personally suggest the concat method since its cleaner and keeps the operations withing the native code of your browser.

function cloneArray( originalArray )
{
return originalArray.concat();
}
As always there is a pre-extended version of this functionality in the Array … Continue Reading