tchmi_clone_object

[ Function ]

function tchmi_clone_object(obj: any): any | null;

Clones an object.

In JavaScript, simple data types (such as Booleans or numbers) are copied when used (by value). However, if a data type is complex, a reference is used (by reference), so that manipulation of the supposed copy also changes the original value.

This reference is desired for variables pointing to HTML elements or TcHmi controls, but not for values such as SolidColor.

The function tchmi_clone_object can be used instead, since it creates a copy in most cases. Exceptions are HTML/SVG elements and TcHmi controls.

Parameter

Name

Type

Description

obj

any

any variable

Return value

Type

Description

any, null

Copy of the passed variable

tchmi_clone_object 1:

Available from 1.8

Sample - JavaScript

var myNumber = 42;
var myNumberAssign = myNumber;
var myNumberClone = tchmi_clone_object(myNumber);
myNumber = 23;
TcHmi.Log.debug(
    'Original: ' + myNumber +
    ' Assign: ' + myNumberAssign +
    ' Clone: ' + myNumberClone); // Original: 23 Assign: 42 Clone: 42

var myColor = {color: 'red'};
var myColorAssign = myColor;
var myColorClone = tchmi_clone_object(myColor);
myColor.color = 'blue';
TcHmi.Log.debug(
    'Original: ' + myColor.color +
    ' Assign: ' + myColorAssign.color +
    ' Clone: ' + myColorClone.color); // Original: blue Assign: blue Clone: red