class method Object.clone

View source on GitHub →

Object.clone(object) → Object
  • object (Object) – The object to clone.

Creates and returns a shallow duplicate of the passed object by copying all of the original's key/value pairs onto an empty object.

Do note that this is a shallow copy, not a deep copy. Nested objects will retain their references.

var original = {name: 'primaryColors', values: ['red', 'green', 'blue']};
var copy = Object.clone(original);;
// -> "primaryColors"
// -> "red";
// -> "primaryColors" = "secondaryColors";;
// -> "primaryColors";
// -> "secondaryColors"
 copy.values[0] = 'magenta';
copy.values[1] = 'cyan';
copy.values[2] = 'yellow';
// -> "magenta" (it's a shallow copy, so they share the array)