removes opt_this from ol.Collection#forEach

this commit also removes all uses of the standard thisArg of
Array#forEach.
This commit is contained in:
simonseyock
2017-12-16 13:43:46 +01:00
parent 1aa7313a7b
commit 3e82c37bed
13 changed files with 28 additions and 36 deletions

View File

@@ -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`

View File

@@ -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.<T>): *} f The function to call
* @param {function(T, number, Array.<T>): *} 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);
}
};

View File

@@ -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,
/**

View File

@@ -129,7 +129,7 @@ var OverviewMap = function(opt_options) {
*/
function(layer) {
ovmap.addLayer(layer);
}, this);
}.bind(this));
}
var box = document.createElement('DIV');

View File

@@ -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,

View File

@@ -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));
}
};

View File

@@ -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;

View File

@@ -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));
};

View File

@@ -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);
}

View File

@@ -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();

View File

@@ -172,7 +172,7 @@ var _ol_reproj_Triangulation_ = function(sourceProj, targetProj, targetExtent,
triangle.source = newTriangle;
}
}
}, this);
}.bind(this));
}
transformInvCache = {};

View File

@@ -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));
}
};

View File

@@ -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() {