Permalink
Browse files

Issue #123. _.extend shouldn't copy keys for undefined values.

  • Loading branch information...
jashkenas committed Apr 15, 2011
1 parent bf214d2 commit ea44179d810ee8e551d783056d989c2b29de0ad2
Showing with 8 additions and 2 deletions.
  1. +2 −0 test/objects.js
  2. +6 −2 underscore.js
View
@@ -33,6 +33,8 @@ $(document).ready(function() {
ok(_.isEqual(result, {x:'x', a:'a', b:'b'}), 'can extend from multiple source objects');
result = _.extend({x:'x'}, {a:'a', x:2}, {a:'b'});
ok(_.isEqual(result, {x:2, a:'b'}), 'extending from multiple source objects last property trumps');
+ result = _.extend({}, {a: void 0, b: null});
+ equals(_.keys(result).join(''), 'b', 'extend does not copy undefined values');
});
test("objects: defaults", function() {
View
@@ -543,15 +543,19 @@
// Extend a given object with all the properties in passed-in object(s).
_.extend = function(obj) {
each(slice.call(arguments, 1), function(source) {
- for (var prop in source) obj[prop] = source[prop];
+ for (var prop in source) {
+ if (source[prop] !== void 0) obj[prop] = source[prop];
+ }
});
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];
+ for (var prop in source) {
+ if (obj[prop] == null) obj[prop] = source[prop];
+ }
});
return obj;
};

0 comments on commit ea44179

Please sign in to comment.