Salient Solutions

wrasslin ones and nones for fun and profit - Sky Sanders' Blog
Get your own ranked flair here
posts - 92, comments - 102, trackbacks - 0

JavaScript 'extend' implementation: copy members from one object to another

 

function extend(target, deep, source /*, source, ... */) {
    /// <summary>Copies the properties of one or more objects to target.</summary>
    /// <param name="target" type="Object">The object upon which to copy the properties of source object(s)</param>
    /// <param name="deep" type="Boolean">If true, deep copy using recursion.</param>
    /// <param name="source" type="Object">Multiple source objects are supported.</param>
    var source;
    for (var argIndex = 2, argCount = arguments.length; argIndex < argCount; argIndex++)
        if ((source = arguments[argIndex]) != null) {
            for (var key in source) {
                if (target == source[key]) {
                    // prevent stackoverflow due to recursion
                    continue;
                }
                if (deep && typeof source[key] == 'object' && target[i]) {
                    extend(target[key], source[key]);
                }
                else if (source[key] != undefined) {
                    target[key] = source[key];
                }
            }
        }
    return target;
}

 


Technorati tags:

Print | posted on Thursday, April 22, 2010 11:16 AM |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 4 and type the answer here:

Powered by: