Object.prototype.extend

by antelopelovefan

JavaScript

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);