Created
August 7, 2013 19:02
-
-
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.
This file contains hidden or 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
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