Merge two object literals in javascript -
i have 2 object literals:
var animal = { eat: function() { console.log("eating..."); } } var dog = { eat: "this has replaced when merged", nroflegs: 4 }
need merging function this:
dog = somemergingfunction(animal, dog);
that produces:
{ eat: function() { console.log("eating..."); }, nroflegs: 4 }
one of object literals has replace identical properties.
how do in javascript?
// usage merged = somemergingfunction(a, b, c, d, ...) // keys in earlier args override keys in later args. // somemergingfunction({foo:"bar"}, {foo:"baz"}) // ==> {foo:"bar"} function somemergingfunction () { var o = {} (var = arguments.length - 1; >= 0; --) { var s = arguments[i] (var k in s) o[k] = s[k] } return o }
Comments
Post a Comment