Skip to content

Instantly share code, notes, and snippets.

@james-gardner
Forked from creynders/jsbin.kikuf.js
Last active August 29, 2015 14:13
Show Gist options
  • Save james-gardner/00271db904052bcf0a0e to your computer and use it in GitHub Desktop.
Save james-gardner/00271db904052bcf0a0e to your computer and use it in GitHub Desktop.
console.clear();
var dest = {
foo : {
b1 : "b1 value",
b2 : "b2 value"
},
baz : {
q1 : "q1 value"
},
mofo : "mofo value"
};
var src = {
foo : {
b1: "overwritten b1",
b3: "b3 value"
},
mofo : "overwritten mofo"
};
var assigned = _.clone(dest);
_.assign(assigned,src);
console.log("assign:", assigned);
var merged = _.clone(dest);
_.merge(merged,src);
console.log("merge:", merged);
var defaulted = _.clone(dest);
_.defaults(defaulted,src);
console.log("defaults:", defaulted);
"assign:"
[object Object] {
baz: [object Object] {
q1: "q1 value"
},
foo: [object Object] {
b1: "overwritten b1",
//--> b2 removed
b3: "b3 value"
},
//--> mofo overwritten
mofo: "overwritten mofo"
}
"merge:"
[object Object] {
baz: [object Object] {
q1: "q1 value"
},
foo: [object Object] {
b1: "overwritten b1",
//--> b2 copied
b2: "b2 value",
b3: "b3 value"
},
//--> mofo overwritten
mofo: "overwritten mofo"
}
"defaults:"
[object Object] {
baz: [object Object] {
q1: "q1 value"
},
foo: [object Object] {
b1: "overwritten b1",
//--> b2 copied
b2: "b2 value",
b3: "b3 value"
},
//--> mofo NOT overwritten
mofo: "mofo value"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment