Adding _.defaults, Issue #106

This commit is contained in:
Jeremy Ashkenas
2011-02-01 21:19:58 -05:00
parent d0faeb53c1
commit 226b7d9344
3 changed files with 37 additions and 1 deletions

View File

@@ -525,6 +525,14 @@
return obj;
};
// Fill in a given object with default properties.
_.defaults = function(obj) {
each(slice.call(arguments, 1), function(source) {
for (var prop in source) if (obj[prop] == null) obj[prop] = source[prop];
});
return obj;
};
// Create a (shallow-cloned) duplicate of an object.
_.clone = function(obj) {
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);