aureooms/js-object

View on GitHub
js/src/deepcopy.js

Summary

Maintainability
A
35 mins
Test Coverage

/**
 * http://stackoverflow.com/a/122190/1582182
 */

var deepcopy = function ( obj , dest ) {

    var key ;

    if ( obj === null || typeof( obj ) !== "object" ) {
        return obj ;
    }

    if ( dest === undefined ) {
        dest = obj.constructor() ;
    }

    for ( key in obj ) {
        
        if ( obj.hasOwnProperty( key ) ) {
            dest[key] = deepcopy( obj[key] ) ;
        }

    }

    return dest;

} ;

exports.deepcopy = deepcopy ;