Skip to content

Instantly share code, notes, and snippets.

@polotek
Created January 1, 2011 20:38
Show Gist options
  • Save polotek/761992 to your computer and use it in GitHub Desktop.
Save polotek/761992 to your computer and use it in GitHub Desktop.
A version of underscore extend method that does deep copy with own properties
_.extend = function(target) {
var i = 1, length = arguments.length, source;
for ( ; i < length; i++ ) {
// Only deal with defined values
if ( (source = arguments[i]) !== undefined ) {
Object.getOwnPropertyNames(source).forEach(function(k){
var d = Object.getOwnPropertyDescriptor(source, k) || {value:source[k]};
if (d.get) {
target.__defineGetter__(k, d.get);
if (d.set) target.__defineSetter__(k, d.set);
}
else if (target !== d.value) {
target[k] = d.value;
}
});
}
}
return target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment