Description

Returns a clone, also known as a deep copy, of a variable. There is no reference to the original variable.

Returns

A clone of a variable.

Category

Structure functionsSystem functions

Function syntax

Duplicate(object)

See also

StructCopy, other Structure functionsModifying a ColdFusion XML object in the Developing ColdFusion Applications

History

ColdFusion (2018 release): Introduced named parameters.

ColdFusion 8: Changed behavior: this function can duplicate CFCs.

ColdFusion MX: Changed behavior: this function can be used on XML objects.

Parameters

Parameter

Description

object

Name of a variable to duplicate

Usage

Use this function to duplicate complex structures, such as nested structures and queries.When you duplicate a CFC instance, the entire CFC contents is copied, including the values of the variables in the 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, have no effect on the other copy.

Note: With this function, you cannot duplicate a COM, CORBA, or JAVA object returned from the cfobject tag or the CreateObject function. If an array element or structure field is a COM, CORBA, or JAVA object, you cannot duplicate the array or structure.

Example

<cfscript>
    s1 = StructNew()
    s1.nested = StructNew()
    s1.nested.item = "original"
    copy = StructCopy(s1)
    clone = Duplicate(s1)
    // modify the original struct
    s1.nested.item = "modified"
    writeOutput("The copy contains the modified value: " & copy.nested.item & "<br/>")
    writeOutput("The duplicate contains the original value: " & clone.nested.item)
</cfscript>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy