diff --git a/changelog/upgrade-notes.md b/changelog/upgrade-notes.md index 86fd03366e..fd31ad4fec 100644 --- a/changelog/upgrade-notes.md +++ b/changelog/upgrade-notes.md @@ -4,6 +4,16 @@ #### Backwards incompatible changes +#### Removal of optional this arguments + +The optional this (i.e. opt_this) arguments were removed from the following methods. +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). + +* `forEachCorner` in `ol/extent` +* `LRUCache#forEach` +* `RBush#forEach` and `RBush#forEachInExtent` + ##### The `setCenter`, `setZoom`, `setResolution` and `setRotation` methods on `ol/View` do not bypass constraints anymore Previously, these methods allowed setting values that were inconsistent with the given view constraints. diff --git a/src/ol/TileCache.js b/src/ol/TileCache.js index 701e48e076..e71cfd4bc3 100644 --- a/src/ol/TileCache.js +++ b/src/ol/TileCache.js @@ -44,7 +44,7 @@ class TileCache extends LRUCache { this.remove(getKey(tile.tileCoord)); tile.dispose(); } - }, this); + }.bind(this)); } } diff --git a/src/ol/extent.js b/src/ol/extent.js index 9858190746..cc74b803fb 100644 --- a/src/ol/extent.js +++ b/src/ol/extent.js @@ -400,26 +400,25 @@ export function extendXY(extent, x, y) { * callback returns a truthy value the function returns that value * immediately. Otherwise the function returns `false`. * @param {Extent} extent Extent. - * @param {function(this:T, import("./coordinate.js").Coordinate): S} callback Callback. - * @param {T=} opt_this Value to use as `this` when executing `callback`. + * @param {function(import("./coordinate.js").Coordinate): S} callback Callback. * @return {S|boolean} Value. - * @template S, T + * @template S */ -export function forEachCorner(extent, callback, opt_this) { +export function forEachCorner(extent, callback) { let val; - val = callback.call(opt_this, getBottomLeft(extent)); + val = callback(getBottomLeft(extent)); if (val) { return val; } - val = callback.call(opt_this, getBottomRight(extent)); + val = callback(getBottomRight(extent)); if (val) { return val; } - val = callback.call(opt_this, getTopRight(extent)); + val = callback(getTopRight(extent)); if (val) { return val; } - val = callback.call(opt_this, getTopLeft(extent)); + val = callback(getTopLeft(extent)); if (val) { return val; } diff --git a/src/ol/geom/Circle.js b/src/ol/geom/Circle.js index 5b38988a77..7ee4ffab44 100644 --- a/src/ol/geom/Circle.js +++ b/src/ol/geom/Circle.js @@ -143,7 +143,7 @@ class Circle extends SimpleGeometry { return true; } - return forEachCorner(extent, this.intersectsCoordinate, this); + return forEachCorner(extent, this.intersectsCoordinate.bind(this)); } return false; diff --git a/src/ol/source/Vector.js b/src/ol/source/Vector.js index b4c26b076f..e44191b9fa 100644 --- a/src/ol/source/Vector.js +++ b/src/ol/source/Vector.js @@ -485,7 +485,7 @@ class VectorSource extends Source { } } else { if (this.featuresRtree_) { - this.featuresRtree_.forEach(this.removeFeatureInternal, this); + this.featuresRtree_.forEach(this.removeFeatureInternal.bind(this)); for (const id in this.nullGeometryFeatures_) { this.removeFeatureInternal(this.nullGeometryFeatures_[id]); } diff --git a/src/ol/structs/LRUCache.js b/src/ol/structs/LRUCache.js index f79ef36baa..29627c6119 100644 --- a/src/ol/structs/LRUCache.js +++ b/src/ol/structs/LRUCache.js @@ -96,17 +96,15 @@ class LRUCache extends EventTarget { /** - * @param {function(this: S, T, string, LRUCache): ?} f The function + * @param {function(T, string, LRUCache): ?} f The function * to call for every entry from the oldest to the newer. This function takes * 3 arguments (the entry value, the entry key and the LRUCache object). * The return value is ignored. - * @param {S=} opt_this The object to use as `this` in `f`. - * @template S */ - forEach(f, opt_this) { + forEach(f) { let entry = this.oldest_; while (entry) { - f.call(opt_this, entry.value_, entry.key_, this); + f(entry.value_, entry.key_, this); entry = entry.newer; } } diff --git a/src/ol/structs/RBush.js b/src/ol/structs/RBush.js index 8d2d8fa6f1..4c0ae6c41b 100644 --- a/src/ol/structs/RBush.js +++ b/src/ol/structs/RBush.js @@ -156,41 +156,35 @@ class RBush { * Calls a callback function with each value in the tree. * If the callback returns a truthy value, this value is returned without * checking the rest of the tree. - * @param {function(this: S, T): *} callback Callback. - * @param {S=} opt_this The object to use as `this` in `callback`. + * @param {function(T): *} callback Callback. * @return {*} Callback return value. - * @template S */ - forEach(callback, opt_this) { - return this.forEach_(this.getAll(), callback, opt_this); + forEach(callback) { + return this.forEach_(this.getAll(), callback); } /** * Calls a callback function with each value in the provided extent. * @param {import("../extent.js").Extent} extent Extent. - * @param {function(this: S, T): *} callback Callback. - * @param {S=} opt_this The object to use as `this` in `callback`. + * @param {function(T): *} callback Callback. * @return {*} Callback return value. - * @template S */ - forEachInExtent(extent, callback, opt_this) { - return this.forEach_(this.getInExtent(extent), callback, opt_this); + forEachInExtent(extent, callback) { + return this.forEach_(this.getInExtent(extent), callback); } /** * @param {Array} values Values. - * @param {function(this: S, T): *} callback Callback. - * @param {S=} opt_this The object to use as `this` in `callback`. + * @param {function(T): *} callback Callback. * @private * @return {*} Callback return value. - * @template S */ - forEach_(values, callback, opt_this) { + forEach_(values, callback) { let result; for (let i = 0, l = values.length; i < l; i++) { - result = callback.call(opt_this, values[i]); + result = callback(values[i]); if (result) { return result; } diff --git a/test/spec/ol/extent.test.js b/test/spec/ol/extent.test.js index e0984eb4b9..eb59120a2e 100644 --- a/test/spec/ol/extent.test.js +++ b/test/spec/ol/extent.test.js @@ -239,13 +239,6 @@ describe('ol.extent', function() { } ); - it('calls the callback with given scope', function() { - const extent = [1, 2, 3, 4]; - const scope = {humpty: 'dumpty'}; - _ol_extent_.forEachCorner(extent, callbackTrue, scope); - expect(callbackTrue.calledOn(scope)).to.be(true); - }); - }); describe('getArea', function() {