Add optional this param to the callback function

This commit is contained in:
Frederic Junod
2015-03-16 11:25:41 +01:00
parent 023816f43b
commit b9aba8babd
2 changed files with 12 additions and 8 deletions
+6 -4
View File
@@ -9,20 +9,22 @@ goog.provide('ol.geom.flat.segments');
* @param {number} offset Offset. * @param {number} offset Offset.
* @param {number} end End. * @param {number} end End.
* @param {number} stride Stride. * @param {number} stride Stride.
* @param {function(ol.Coordinate, ol.Coordinate): T} callback Function * @param {function(this: S, ol.Coordinate, ol.Coordinate): T} callback Function
* called for each segment. * called for each segment.
* @param {S=} opt_this The object to be used as the value of 'this'
* within callback.
* @return {T|boolean} Value. * @return {T|boolean} Value.
* @template T * @template T,S
*/ */
ol.geom.flat.segments.forEach = ol.geom.flat.segments.forEach =
function(flatCoordinates, offset, end, stride, callback) { function(flatCoordinates, offset, end, stride, callback, opt_this) {
var point1 = [flatCoordinates[offset], flatCoordinates[offset + 1]]; var point1 = [flatCoordinates[offset], flatCoordinates[offset + 1]];
var point2 = []; var point2 = [];
var ret; var ret;
for (; (offset + stride) < end; offset += stride) { for (; (offset + stride) < end; offset += stride) {
point2[0] = flatCoordinates[offset + stride]; point2[0] = flatCoordinates[offset + stride];
point2[1] = flatCoordinates[offset + stride + 1]; point2[1] = flatCoordinates[offset + stride + 1];
ret = callback(point1, point2); ret = callback.call(opt_this, point1, point2);
if (ret) { if (ret) {
return ret; return ret;
} }
+6 -4
View File
@@ -113,15 +113,17 @@ ol.geom.LineString.prototype.closestPointXY =
* If the callback returns a truthy value the function returns that * If the callback returns a truthy value the function returns that
* value immediately. Otherwise the function returns `false`. * value immediately. Otherwise the function returns `false`.
* *
* @param {function(ol.Coordinate, ol.Coordinate): T} callback Function * @param {function(this: S, ol.Coordinate, ol.Coordinate): T} callback Function
* called for each segment. * called for each segment.
* @param {S=} opt_this The object to be used as the value of 'this'
* within callback.
* @return {T|boolean} Value. * @return {T|boolean} Value.
* @template T * @template T,S
* @api * @api
*/ */
ol.geom.LineString.prototype.forEachSegment = function(callback) { ol.geom.LineString.prototype.forEachSegment = function(callback, opt_this) {
return ol.geom.flat.segments.forEach(this.flatCoordinates, 0, return ol.geom.flat.segments.forEach(this.flatCoordinates, 0,
this.flatCoordinates.length, this.stride, callback); this.flatCoordinates.length, this.stride, callback, opt_this);
}; };