Condividi tramite


Array.clone Function

Creates a shallow copy of an Array object. This function is static and can be invoked without creating an instance of the object.

var cloneVar = Array.clone(array);

Arguments

Term

Definition

array

The array to create a shallow copy of.

Remarks

Use the clone function to create a shallow copy of an Array object. A shallow copy contains only the elements of the array, whether they are reference types or value types. However, it does not contain the referenced objects. The references in the new Array object point to the same objects as in the original Array object. In contrast, a deep copy of an Array object copies the elements and everything directly or indirectly referenced by the elements.

Note

In Mozilla Firefox version 2.0.0.1 and earlier, the Array.addRange and Array.clone functions might drop elements from the ends of large, sparse arrays.

Example

The following code example demonstrates how to create a copy of an array using the clone function.

var a = ['a', 'b', 'c', 'd'];
var b = Array.clone(a);
// View the results: "abcd"
alert(b.toString());

See Also

Reference

Array Object

Array Type Extensions

Other Resources

Language Reference