Skip to content

Instantly share code, notes, and snippets.

@EyePulp
Created January 12, 2011 17:26
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 EyePulp/776508 to your computer and use it in GitHub Desktop.
Save EyePulp/776508 to your computer and use it in GitHub Desktop.
adding .extend()
Object.defineProperty(Object.prototype, "extend", {
enumerable: false,
value: function(from) {
var props = Object.getOwnPropertyNames(from);
var dest = this;
props.forEach(function(name) {
if (name in dest) {
var destination = Object.getOwnPropertyDescriptor(from, name);
Object.defineProperty(dest, name, destination);
}
});
return this;
}
});
//usage:
var foo = {'a':1,'b':2};
var bar = {'a':3};
console.log(foo.extend(bar));
// {'a':3,'b':2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment