From 12c7cb07b23ea3adcbb9313a9673b2ce15ad055d Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 26 Nov 2007 23:45:43 +0000 Subject: [PATCH] extend now only sets defined properties on the destination - if your source has a property set to undefined, the destination property will not be set (closes #1160). git-svn-id: http://svn.openlayers.org/trunk/openlayers@5281 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Util.js | 8 ++++++-- tests/test_Util.html | 9 ++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/OpenLayers/Util.js b/lib/OpenLayers/Util.js index 1743b56a28..6fd439e561 100644 --- a/lib/OpenLayers/Util.js +++ b/lib/OpenLayers/Util.js @@ -38,7 +38,8 @@ if ($ == null) { /** * APIFunction: extend * Copy all properties of a source object to a destination object. Modifies - * the passed in destination object. + * the passed in destination object. Any properties on the source object + * that are set to undefined will not be (re)set on the destination object. * * Parameters: * destination - {Object} The object that will be modified @@ -50,7 +51,10 @@ if ($ == null) { OpenLayers.Util.extend = function(destination, source) { if(destination && source) { for(var property in source) { - destination[property] = source[property]; + value = source[property]; + if(value !== undefined) { + destination[property] = value; + } } /** * IE doesn't include the toString property when iterating over an object's diff --git a/tests/test_Util.html b/tests/test_Util.html index 8885d8fcde..fb4659c32d 100644 --- a/tests/test_Util.html +++ b/tests/test_Util.html @@ -591,7 +591,7 @@ } function tests_Util_extend(t) { - t.plan(5); + t.plan(6); var source = { num: Math.random(), @@ -603,9 +603,10 @@ }, toString: function() { return "source"; - } + }, + nada: undefined }; - var destination = OpenLayers.Util.extend({}, source); + var destination = OpenLayers.Util.extend({nada: "untouched"}, source); t.eq(destination.num, source.num, "extend properly sets primitive property on destination"); t.eq(destination.obj, source.obj, @@ -614,6 +615,8 @@ "extend properly sets function property on destination"); t.eq(destination.toString(), "source", "extend properly sets custom toString method"); + t.eq(destination.nada, "untouched", + "undefined source properties don't clobber existing properties"); t.eq(window.property, undefined, "Property variable not clobbered."); }