From 964bbf41bd309892f0294087f7d1009d7d4f8283 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 12 Jul 2012 19:23:06 +0200 Subject: [PATCH] Add scope to ol.Array.forEach --- src/ol/array.js | 8 +++++--- src/ol/array_test.js | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/ol/array.js b/src/ol/array.js index b397baa073..a5b7dbbded 100644 --- a/src/ol/array.js +++ b/src/ol/array.js @@ -104,10 +104,12 @@ ol.Array.prototype.clear = function() { /** - * @param {function(*, number)} callback Callback. + * @param {function(this: T, *, number)} f Function. + * @param {T=} opt_obj The object to be used for the value of 'this' within f. + * @template T */ -ol.Array.prototype.forEach = function(callback) { - goog.array.forEach(this.array_, callback); +ol.Array.prototype.forEach = function(f, opt_obj) { + goog.array.forEach(this.array_, f, opt_obj); }; diff --git a/src/ol/array_test.js b/src/ol/array_test.js index 1d77583d5d..580c908301 100644 --- a/src/ol/array_test.js +++ b/src/ol/array_test.js @@ -192,3 +192,24 @@ function testLengthChangeSetAt() { array.setAt(1, 1); assertUndefined(lengthChangedCalled); } + + +function testForEach() { + var array = ol.Array.create([1, 2, 4]); + var sum = 0; + array.forEach(function(elem) { + sum += elem; + }); + assertEquals(7, sum); +} + + +function testForEachScope() { + var array = ol.Array.create([0]); + var that; + var uniqueObj = {}; + array.forEach(function(elem) { + that = this; + }, uniqueObj); + assertTrue(that === uniqueObj); +}