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
This commit is contained in:
Tim Schaub
2007-11-26 23:45:43 +00:00
parent d7905ec735
commit 12c7cb07b2
2 changed files with 12 additions and 5 deletions

View File

@@ -38,7 +38,8 @@ if ($ == null) {
/** /**
* APIFunction: extend * APIFunction: extend
* Copy all properties of a source object to a destination object. Modifies * 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: * Parameters:
* destination - {Object} The object that will be modified * destination - {Object} The object that will be modified
@@ -50,7 +51,10 @@ if ($ == null) {
OpenLayers.Util.extend = function(destination, source) { OpenLayers.Util.extend = function(destination, source) {
if(destination && source) { if(destination && source) {
for(var property in 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 * IE doesn't include the toString property when iterating over an object's

View File

@@ -591,7 +591,7 @@
} }
function tests_Util_extend(t) { function tests_Util_extend(t) {
t.plan(5); t.plan(6);
var source = { var source = {
num: Math.random(), num: Math.random(),
@@ -603,9 +603,10 @@
}, },
toString: function() { toString: function() {
return "source"; return "source";
} },
nada: undefined
}; };
var destination = OpenLayers.Util.extend({}, source); var destination = OpenLayers.Util.extend({nada: "untouched"}, source);
t.eq(destination.num, source.num, t.eq(destination.num, source.num,
"extend properly sets primitive property on destination"); "extend properly sets primitive property on destination");
t.eq(destination.obj, source.obj, t.eq(destination.obj, source.obj,
@@ -614,6 +615,8 @@
"extend properly sets function property on destination"); "extend properly sets function property on destination");
t.eq(destination.toString(), "source", t.eq(destination.toString(), "source",
"extend properly sets custom toString method"); "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."); t.eq(window.property, undefined, "Property variable not clobbered.");
} }