Duplicate
Duplicates an object - either shallow or deep
Method Signature
Duplicate(object=[any], deep=[boolean])
Arguments
Argument
Type
Required
Description
Default
object
any
true
Any object to duplicate
deep
boolean
false
Whether to deep copy the object or make a shallow copy (e.g. only the top level keys in a struct)
true
Examples
Changing a struct compared to changing its copy
myNewStruct
holds a reference to myStruct
so if you change myNewStruct
, myStruct
is changed accordingly as well, because they are the same struct just assigned to two variables.
In comparison myOtherNewStruct
is a copy so if you change myOtherNewStruct
, myStruct
stays untouched because with the duplicate, a new, unique structure with the same key-value pairs is created thus they do not share the same reference
myStruct = {
"foo" : "Lorem ipsum",
"bar" : "baz"
};
myNewStruct = myStruct;
myOtherNewStruct = duplicate( myStruct );
myNewStruct.FOO = "ahmet sun";
myOtherNewStruct.FOO = "dolor sit";
writeOutput( myStruct.FOO & " → " & myNewStruct.FOO & " → " & myOtherNewStruct.FOO );
Result: ahmet sun → ahmet sun → dolor sit
Additional Examples
person = {
FIRST : "Babe",
LAST : "Ruth"
};
dump( person ); // Babe Ruth
clone = duplicate( person );
dump( clone ); // Babe Ruth
person.LAST = "Smith";
dump( person ); // Babe Smith
dump( clone );
// Babe Ruth
Related
Last updated
Was this helpful?