diff --git a/lib/OpenLayers/Util.js b/lib/OpenLayers/Util.js index 8727e0baf4..ac8874b1ff 100644 --- a/lib/OpenLayers/Util.js +++ b/lib/OpenLayers/Util.js @@ -36,12 +36,30 @@ if ($ == null) { } /** - * Function: extend - * From Prototype.js + * APIFunction: extend + * Copy all properties of a source object to a destination object. Modifies + * the passed in destination object. + * + * Parameters: + * destination - {Object} The object that will be modified + * source - {Object} The object with properties to be set on the destination + * + * Returns: + * {Object} The destination object. */ OpenLayers.Util.extend = function(destination, source) { - for (property in source) { - destination[property] = source[property]; + if(destination && source) { + for(property in source) { + destination[property] = source[property]; + } + /** + * IE doesn't include the toString property when iterating over an object's + * properties with the for(property in object) syntax. Explicitly check if + * the source has its own toString property. + */ + if(source.hasOwnProperty && source.hasOwnProperty('toString')) { + destination.toString = source.toString; + } } return destination; }; diff --git a/tests/test_Util.html b/tests/test_Util.html index 846407d51e..cb263769bf 100644 --- a/tests/test_Util.html +++ b/tests/test_Util.html @@ -578,7 +578,33 @@ t.eq(OpenLayers.Util.getArgs('http://www.example.com?foo=bar#bugssucks'), {'foo': 'bar'}, "getArgs works when using a fragment identifier"); } - // --> + + function tests_Util_extend(t) { + t.plan(4); + var source = { + num: Math.random(), + obj: { + foo: "bar" + }, + method: function() { + return "method"; + }, + toString: function() { + return "source"; + } + }; + var destination = OpenLayers.Util.extend({}, source); + t.eq(destination.num, source.num, + "extend properly sets primitive property on destination"); + t.eq(destination.obj, source.obj, + "extend properly sets object property on destination"); + t.eq(destination.method(), "method", + "extend properly sets function property on destination"); + t.eq(destination.toString(), "source", + "extend properly sets custom toString method"); + } + + // -->