Created
October 4, 2014 18:46
-
-
Save weslleyaraujo/ebaa05ae7d5f04292b63 to your computer and use it in GitHub Desktop.
Merge objects
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
Object.defineProperty(Object.prototype, 'extend', { | |
enumerable: false, | |
value: function (from) { | |
var props = Object.getOwnPropertyNames(from), | |
dest = this; | |
props.forEach(function(name) { | |
var destination; | |
if (name in dest) { | |
destination = Object.getOwnPropertyDescriptor(from, name); | |
Object.defineProperty(dest, name, destination); | |
return; | |
} | |
Object.defineProperty(dest, name, { | |
value: from[name] | |
}); | |
}); | |
return this; | |
} | |
}); | |
var obj = { | |
name: 'weslley' | |
}, | |
x = { | |
lastName: 'Araujo' | |
}; | |
obj.extend(x); // { name: 'Weslley', lastName: 'Araujo' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment