How To The Write `duplicate( object )` Function In ColdFusion?
Description:-
This is used to clone or deep copy of an object or variable, leaving no reference to the original. Use this function to duplicate complex structures, such as nested structures and queries. When you duplicate a CFC instance, all of the CFC contents are copied, including the values of the variables in this scope at the time you call the Duplicate function. Thereafter, the two CFC instances are independent, and changes to one copy, for example by calling one of its functions, do not affect the other copy.
duplicate( object );
This function returns a clone of the given variable or object.
Attributes:-
object:-
This is a required argument, an object or variable that needs to be cloned or copied.
Example:-
<cfscript>
test = {
"a" : 100,
"b" : 200
};
writeDump( "Old Structure:" );
writeDump( test );
writeDump( "Old Structure:" );
writeDump( duplicate( test ) );
</cfscript>
Result:-
Old Structure:
struct
a 100
b 200
Old Structure:
struct
a 100
b 200
In this example, we create a copy of test
structures variable then shows the output after and before cloning or copying.