Underscore has this incredibly useful function called extend. This extends the components of an object - plain and simple. On the site it provides the example:
_.extend({name: 'moe'}, {age: 50});Which outputs:
=> {name: 'moe', age: 50}However, this updates the original object! Consider the following example:
var dude = {
underwear : true,
pants: false
};
var attire = {
pants: 'jeans',
shirt: 't-shirt'
}
_.extend(dude, attire);
/*
console.log(dude)
{
underwear : true,
pants: 'jeans',
shirt: 't-shirt'
}
*/In the above example, the dude will forever be wearing pants! Solution?
var dudeAtHome = {
underwear : true,
pants: false
};
var attire = {
pants: 'jeans',
shirt: 't-shirt'
}
dudeInPublic = _.extend({}, dude, attire);
/*
console.log(dudeInPublic)
{
underwear : true,
pants: 'jeans',
shirt: 't-shirt'
}
console.log(dudeAtHome)
{
underwear : true,
pants: false
}
*/Now, we have a extended object, while still preserving the original.
Pretty sweet.