Add scope to ol.Array.forEach

This commit is contained in:
Tom Payne
2012-07-12 19:23:06 +02:00
parent 7ae4fc3eb5
commit 964bbf41bd
2 changed files with 26 additions and 3 deletions

View File

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

View File

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