Created
January 1, 2011 20:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.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