From ea44179d810ee8e551d783056d989c2b29de0ad2 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 15 Apr 2011 17:46:16 -0400 Subject: [PATCH] Issue #123. _.extend shouldn't copy keys for undefined values. --- test/objects.js | 2 ++ underscore.js | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/test/objects.js b/test/objects.js index 67d05ecf2..93c441176 100644 --- a/test/objects.js +++ b/test/objects.js @@ -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() { diff --git a/underscore.js b/underscore.js index 981a7ce8f..c24c34645 100644 --- a/underscore.js +++ b/underscore.js @@ -543,7 +543,9 @@ // 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; }; @@ -551,7 +553,9 @@ // 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; };