Created
June 11, 2012 03:48
-
-
Save linyows/2908423 to your computer and use it in GitHub Desktop.
javascript hash merge
This file contains 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
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