Javascript Deep Clone Object with Circular References
<p>I have copied the function below from an <a href="https://stackoverflow.com/questions/4459928/how-to-deep-clone-in-javascript">existing answer</a> by Dmitriy Pichugin. This function can deep clone an object without any circular references- it works.</p>

<pre><code>function deepClone( obj ) {
    if( !obj || true == obj ) //this also handles boolean as true and false
        return obj;
    var objType = typeof( obj );
    if( "number" == objType || "string" == objType ) // add your immutables here
        return obj;
    var result = Array.isArray( obj ) ? [] : !obj.constructor ? {} : new obj.constructor();
    if( obj instanceof Map )
        for( var key of obj.keys() )
            result.set( key, deepClone( obj.get( key ) ) );
    for( var key in obj )
    if( obj.hasOwnProperty( key ) )
            result[key] = deepClone( obj[ key ] );
    return result;
}
</code></pre>

<p>However, my program loops indefinitely and I have realised that this is due to a circular reference.</p>

<p>An example of a circular reference:</p>

<pre><code>function A() {}
function B() {}

var a = new A();
var b = new B();

a.b = b;
b.a = a;
</code></pre>