diff --git a/changelog/upgrade-notes.md b/changelog/upgrade-notes.md index edf992bf7e..cd017c42a8 100644 --- a/changelog/upgrade-notes.md +++ b/changelog/upgrade-notes.md @@ -35,6 +35,12 @@ register(proj4); The map and sources no longer accept a `logo` option. Instead, if you wish to append a logo to your map, add the desired markup directly in your HTML. In addition, you can use the `attributions` property of a source to display arbitrary markup per-source with the attribution control. +#### Removal of optional this arguments. + +The following methods did get the optional this (i.e. opt_this) arguments removed. Please use closures, the es6 arrow function or the bind method to achieve this effect (Bind is explained here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind). + +* ol.Collection#forEach + ### v4.6.0 #### Renamed `exceedLength` option of `ol.style.Text` to `overflow` diff --git a/src/ol/Collection.js b/src/ol/Collection.js index a0a1d9ae95..dbbdd4c24b 100644 --- a/src/ol/Collection.js +++ b/src/ol/Collection.js @@ -101,18 +101,15 @@ _ol_Collection_.prototype.extend = function(arr) { /** * Iterate over each element, calling the provided callback. - * @param {function(this: S, T, number, Array.): *} f The function to call + * @param {function(T, number, Array.): *} f The function to call * for every element. This function takes 3 arguments (the element, the * index and the array). The return value is ignored. - * @param {S=} opt_this The object to use as `this` in `f`. - * @template S * @api */ -_ol_Collection_.prototype.forEach = function(f, opt_this) { - var fn = (opt_this) ? f.bind(opt_this) : f; +_ol_Collection_.prototype.forEach = function(f) { var array = this.array_; for (var i = 0, ii = array.length; i < ii; ++i) { - fn(array[i], i, array); + f(array[i], i, array); } }; diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index fd944f22cf..67be92077d 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -357,7 +357,7 @@ var _ol_PluggableMap_ = function(options) { */ function(control) { control.setMap(this); - }, this); + }.bind(this)); _ol_events_.listen(this.controls, CollectionEventType.ADD, /** @@ -382,7 +382,7 @@ var _ol_PluggableMap_ = function(options) { */ function(interaction) { interaction.setMap(this); - }, this); + }.bind(this)); _ol_events_.listen(this.interactions, CollectionEventType.ADD, /** @@ -400,7 +400,7 @@ var _ol_PluggableMap_ = function(options) { event.element.setMap(null); }, this); - this.overlays_.forEach(this.addOverlayInternal_, this); + this.overlays_.forEach(this.addOverlayInternal_.bind(this)); _ol_events_.listen(this.overlays_, CollectionEventType.ADD, /** diff --git a/src/ol/control/OverviewMap.js b/src/ol/control/OverviewMap.js index 0ef3be298b..a59754ad8e 100644 --- a/src/ol/control/OverviewMap.js +++ b/src/ol/control/OverviewMap.js @@ -129,7 +129,7 @@ var OverviewMap = function(opt_options) { */ function(layer) { ovmap.addLayer(layer); - }, this); + }.bind(this)); } var box = document.createElement('DIV'); diff --git a/src/ol/interaction/Modify.js b/src/ol/interaction/Modify.js index e9d33737e5..e162bf0d47 100644 --- a/src/ol/interaction/Modify.js +++ b/src/ol/interaction/Modify.js @@ -213,7 +213,7 @@ var _ol_interaction_Modify_ = function(options) { */ this.features_ = features; - this.features_.forEach(this.addFeature_, this); + this.features_.forEach(this.addFeature_.bind(this)); _ol_events_.listen(this.features_, CollectionEventType.ADD, this.handleFeatureAdd_, this); _ol_events_.listen(this.features_, CollectionEventType.REMOVE, diff --git a/src/ol/interaction/Select.js b/src/ol/interaction/Select.js index 5160d05a7d..cb4bee33ab 100644 --- a/src/ol/interaction/Select.js +++ b/src/ol/interaction/Select.js @@ -313,12 +313,12 @@ _ol_interaction_Select_.prototype.setMap = function(map) { var selectedFeatures = this.featureOverlay_.getSource().getFeaturesCollection(); if (currentMap) { - selectedFeatures.forEach(currentMap.unskipFeature, currentMap); + selectedFeatures.forEach(currentMap.unskipFeature.bind(currentMap)); } Interaction.prototype.setMap.call(this, map); this.featureOverlay_.setMap(map); if (map) { - selectedFeatures.forEach(map.skipFeature, map); + selectedFeatures.forEach(map.skipFeature.bind(map)); } }; diff --git a/src/ol/interaction/Snap.js b/src/ol/interaction/Snap.js index eec5ca78a5..0dff888f79 100644 --- a/src/ol/interaction/Snap.js +++ b/src/ol/interaction/Snap.js @@ -302,7 +302,7 @@ _ol_interaction_Snap_.prototype.setMap = function(map) { if (currentMap) { keys.forEach(_ol_events_.unlistenByKey); keys.length = 0; - features.forEach(this.forEachFeatureRemove_, this); + features.forEach(this.forEachFeatureRemove_.bind(this)); } _ol_interaction_Pointer_.prototype.setMap.call(this, map); @@ -322,7 +322,7 @@ _ol_interaction_Snap_.prototype.setMap = function(map) { this.handleFeatureRemove_, this) ); } - features.forEach(this.forEachFeatureAdd_, this); + features.forEach(this.forEachFeatureAdd_.bind(this)); } }; @@ -613,7 +613,7 @@ _ol_interaction_Snap_.handleEvent_ = function(evt) { _ol_interaction_Snap_.handleUpEvent_ = function(evt) { var featuresToUpdate = _ol_obj_.getValues(this.pendingFeatures_); if (featuresToUpdate.length) { - featuresToUpdate.forEach(this.updateFeature_, this); + featuresToUpdate.forEach(this.updateFeature_.bind(this)); this.pendingFeatures_ = {}; } return false; diff --git a/src/ol/pointer/PointerEventHandler.js b/src/ol/pointer/PointerEventHandler.js index 0b08664cb0..f7dc510e9d 100644 --- a/src/ol/pointer/PointerEventHandler.js +++ b/src/ol/pointer/PointerEventHandler.js @@ -122,7 +122,7 @@ _ol_pointer_PointerEventHandler_.prototype.registerSource = function(name, sourc if (handler) { this.eventMap_[e] = handler.bind(s); } - }, this); + }.bind(this)); this.eventSourceList_.push(s); } }; @@ -178,7 +178,7 @@ _ol_pointer_PointerEventHandler_.prototype.eventHandler_ = function(inEvent) { _ol_pointer_PointerEventHandler_.prototype.addEvents_ = function(events) { events.forEach(function(eventName) { _ol_events_.listen(this.element_, eventName, this.eventHandler_, this); - }, this); + }.bind(this)); }; @@ -190,7 +190,7 @@ _ol_pointer_PointerEventHandler_.prototype.addEvents_ = function(events) { _ol_pointer_PointerEventHandler_.prototype.removeEvents_ = function(events) { events.forEach(function(e) { _ol_events_.unlisten(this.element_, e, this.eventHandler_, this); - }, this); + }.bind(this)); }; diff --git a/src/ol/renderer/webgl/VectorLayer.js b/src/ol/renderer/webgl/VectorLayer.js index cf6c6541e5..5a8ef45505 100644 --- a/src/ol/renderer/webgl/VectorLayer.js +++ b/src/ol/renderer/webgl/VectorLayer.js @@ -292,7 +292,7 @@ _ol_renderer_webgl_VectorLayer_.prototype.prepareFrame = function(frameState, la features.push(feature); }, this); features.sort(vectorLayerRenderOrder); - features.forEach(renderFeature, this); + features.forEach(renderFeature.bind(this)); } else { vectorSource.forEachFeatureInExtent(extent, renderFeature, this); } diff --git a/src/ol/reproj/Tile.js b/src/ol/reproj/Tile.js index 2e826c2438..7feecf1512 100644 --- a/src/ol/reproj/Tile.js +++ b/src/ol/reproj/Tile.js @@ -222,7 +222,7 @@ _ol_reproj_Tile_.prototype.reproject_ = function() { image: tile.getImage() }); } - }, this); + }.bind(this)); this.sourceTiles_.length = 0; if (sources.length === 0) { @@ -281,7 +281,7 @@ _ol_reproj_Tile_.prototype.load = function() { }, this); this.sourcesListenerKeys_.push(sourceListenKey); } - }, this); + }.bind(this)); this.sourceTiles_.forEach(function(tile, i, arr) { var state = tile.getState(); diff --git a/src/ol/reproj/Triangulation.js b/src/ol/reproj/Triangulation.js index 47848b9737..033b0ee8bb 100644 --- a/src/ol/reproj/Triangulation.js +++ b/src/ol/reproj/Triangulation.js @@ -172,7 +172,7 @@ var _ol_reproj_Triangulation_ = function(sourceProj, targetProj, targetExtent, triangle.source = newTriangle; } } - }, this); + }.bind(this)); } transformInvCache = {}; diff --git a/src/ol/source/Vector.js b/src/ol/source/Vector.js index 849670b350..4663256a7c 100644 --- a/src/ol/source/Vector.js +++ b/src/ol/source/Vector.js @@ -390,7 +390,7 @@ _ol_source_Vector_.prototype.forEachFeature = function(callback, opt_this) { if (this.featuresRtree_) { return this.featuresRtree_.forEach(callback, opt_this); } else if (this.featuresCollection_) { - return this.featuresCollection_.forEach(callback, opt_this); + return this.featuresCollection_.forEach(callback.bind(opt_this)); } }; @@ -446,7 +446,7 @@ _ol_source_Vector_.prototype.forEachFeatureInExtent = function(extent, callback, if (this.featuresRtree_) { return this.featuresRtree_.forEachInExtent(extent, callback, opt_this); } else if (this.featuresCollection_) { - return this.featuresCollection_.forEach(callback, opt_this); + return this.featuresCollection_.forEach(callback.bind(opt_this)); } }; diff --git a/test/spec/ol/collection.test.js b/test/spec/ol/collection.test.js index 54546a1f29..7d95628bb7 100644 --- a/test/spec/ol/collection.test.js +++ b/test/spec/ol/collection.test.js @@ -106,17 +106,6 @@ describe('ol.collection', function() { expect(cb.callCount).to.eql(2); }); }); - describe('scope', function() { - it('callbacks get the correct scope', function() { - var collection = new _ol_Collection_([0]); - var that; - var uniqueObj = {}; - collection.forEach(function(elem) { - that = this; - }, uniqueObj); - expect(that).to.be(uniqueObj); - }); - }); }); describe('remove', function() {