Skip to content

Instantly share code, notes, and snippets.

@livingston
Created February 1, 2011 10:00
Show Gist options
  • Save livingston/805654 to your computer and use it in GitHub Desktop.
Save livingston/805654 to your computer and use it in GitHub Desktop.
ES5 Extend & Clone Objects
Object.defineProperties(Object, {
'extend': {
'configurable': true,
'enumerable': false,
'value': function extend(what, wit) {
var extObj, witKeys = Object.keys(wit);
extObj = Object.keys(what).length ? Object.clone(what) : {};
witKeys.forEach(function (key) {
Object.defineProperty(extObj, key, Object.getOwnPropertyDescriptor(wit, key));
});
return extObj;
},
'writable': true
},
'clone': {
'configurable': true,
'enumerable': false,
'value': function clone(obj) {
return Object.extend({}, obj);
},
'writable': true
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment