Skip to content

Instantly share code, notes, and snippets.

@weslleyaraujo
Created October 4, 2014 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weslleyaraujo/ebaa05ae7d5f04292b63 to your computer and use it in GitHub Desktop.
Save weslleyaraujo/ebaa05ae7d5f04292b63 to your computer and use it in GitHub Desktop.
Merge objects
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