common/core.js
/*
    * Make a deep clone of an object, but copy instances of specified constructors.
    * @param {Array} constr An array containing constructors. The method will not clone, but copy instances of these constructors recursively.
    * @param {Object} to The target object that will receive the cloned members.
    * @param {...Object} args The objects to clone and add to the target object.
    */
Make a deep clone of an object, but copy instances of specified constructors.
@param {Array} constr An array containing constructors. The method will not clone, but copy instances of these constructors recursively.
@param {Object} to The target object that will receive the cloned members.
@param {...Object} args The objects to clone and add to the target object.
Make a deep clone of an object but copy instances of specified constructors . @param { Array } constr An array containing constructors . The method will not clone but copy instances of these constructors recursively . @param { Object } to The target object that will receive the cloned members . @param { ... Object } args The objects to clone and add to the target object .
function extendWithCopy(constr) {
        var args = Array.apply(null, arguments),
            hasConstr = is.array(constr) && constr.length &&                        
                !grep(constr, function (con) { return !is.func(con); }).length,   
            to = args[1],
            from, 
            i,
            key, 
            target, 
            value,
            isInstanceOfFunc = function(obj, inst) { return obj instanceof inst; };
        if (!hasConstr) {
            return extend.apply($, args);
        }
        if (!is.object(to) && !is.array(to)) {
            to = {};
        }
        for (i = 2; i < args.length; i++) {
            from = args[i];
            if (from) {
                for (key in from) {
                    if (from.hasOwnProperty(key)) {
                        value = from[key];
                        target = to[key];
                        if (value && grep(constr, proxy(isInstanceOfFunc, null, value)).length) {
                            to[key] = value;
                        }
                        else if (is.object(value) || is.array(value)) {
                            if (!is.object(target)) {
                                target = to[key] = is.array(value) ? [] : {};
                            }
                            to[key] = extendWithCopy(constr, target, value);
                        }
                        else if (is.date(value)) {
                            to[key] = new Date(value.getTime());
                        }
                        else if (value !== undefined) {
                            to[key] = value;
                        }
                    }
                }
            }
        }
        return to;
    }
function extendWithCopy ( constr ) { var args = Array . apply ( null , arguments ) , hasConstr = is . array ( constr ) && constr . length && ! grep ( constr , function ( con ) { return ! is . func ( con ) ; } ) . length , to = args [ 1 ] , from , i , key , target , value , isInstanceOfFunc = function ( obj , inst ) { return obj instanceof inst ; } ; if ( ! hasConstr ) { return extend . apply ( $ , args ) ; } if ( ! is . object ( to ) && ! is . array ( to ) ) { to = { } ; } for ( i = 2 ; i < args . length ; i ++ ) { from = args [ i ] ; if ( from ) { for ( key in from ) { if ( from . hasOwnProperty ( key ) ) { value = from [ key ] ; target = to [ key ] ; if ( value && grep ( constr , proxy ( isInstanceOfFunc , null , value ) ) . length ) { to [ key ] = value ; } else if ( is . object ( value ) || is . array ( value ) ) { if ( ! is . object ( target ) ) { target = to [ key ] = is . array ( value ) ? [ ] : { } ; } to [ key ] = extendWithCopy ( constr , target , value ) ; } else if ( is . date ( value ) ) { to [ key ] = new Date ( value . getTime ( ) ) ; } else if ( value !== undefined ) { to [ key ] = value ; } } } } } return to ; }
Make a deep clone of an object, but copy instances of specified constructors.
Make a deep clone of an object but copy instances of specified constructors .
//constr is non-empty array //all constr items are constructor functions