Skip to content

Instantly share code, notes, and snippets.

@linyows
Created June 11, 2012 03:48
Show Gist options
  • Save linyows/2908423 to your computer and use it in GitHub Desktop.
Save linyows/2908423 to your computer and use it in GitHub Desktop.
javascript hash merge
var a = {aaa: 1, bbb: 2, ccc: 3};
var b = {ddd: 4, eee: 5};
var Hash = function(hash){
this.hash = hash;
};
Hash.prototype.merge = function(target) {
for (var i in target) {
if (typeof target[i] === 'function') { continue; }
if (typeof target[i] === 'object') { continue; }
this.hash[i] = target[i];
}
return this.hash;
};
console.log(new Hash(a).merge(b));
// or
Object.extend = function(destination, source) {
for (var property in source) {
if (source.hasOwnProperty(property)) {
destination[property] = source[property];
}
}
return destination;
};
console.log(Object.extend(a, b));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment