Skip to content

Instantly share code, notes, and snippets.

@markbiek
Created August 7, 2013 19:02
Show Gist options
  • Save markbiek/6177358 to your computer and use it in GitHub Desktop.
Save markbiek/6177358 to your computer and use it in GitHub Desktop.
Simple Javascript function to populate an object with the properties of another. Doesn't take deep objects into account.
if(!Object.hasOwnProperty('extend')) {
Object.prototype.extend = function(obj) {
for(var i in obj) {
if(obj.hasOwnProperty(i) && !this.hasOwnProperty(i)) {
this[i] = obj[i];
}
}
};
}
var dest = { foo: 'bar' };
console.log(dest);
dest.extend({ baz: 'boo'});
console.log(dest);
console.log('');
var dest2 = {baz: 'boo'};
console.log(dest2);
dest2.extend({foo: 'bar'});
console.log(dest2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment