Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

angular.forEach passes object being iterated over as 3rd arg to callback #8108

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ function isArrayLike(obj) {
*
* @description
* Invokes the `iterator` function once for each item in `obj` collection, which can be either an
* object or an array. The `iterator` function is invoked with `iterator(value, key)`, where `value`
* is the value of an object property or an array element and `key` is the object property key or
* array element index. Specifying a `context` for the function is optional.
* object or an array. The `iterator` function is invoked with `iterator(value, key, obj)`, where
* `value` is the value of an object property or an array element, `key` is the object property
* key or array element index, and `obj` is a reference to the collection being iterated over.
* Specifying a `context` for the function is optional.
*
* It is worth noting that `.forEach` does not iterate over inherited properties because it filters
* using the `hasOwnProperty` method.
Expand All @@ -239,20 +240,22 @@ function forEach(obj, iterator, context) {
for (key in obj) {
// Need to check if hasOwnProperty exists,
// as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function
if (key != 'prototype' && key != 'length' && key != 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
iterator.call(context, obj[key], key);
if (key !== 'prototype' && key !== 'length' && key !== 'name' &&
(!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
iterator.call(context, obj[key], key, obj);
}
}
} else if (isArray(obj) || isArrayLike(obj)) {
// for loop is actually faster than native Array.prototype.forEach()
for (key = 0, length = obj.length; key < length; key++) {
iterator.call(context, obj[key], key);
iterator.call(context, obj[key], key, obj);
}
} else if (obj.forEach && obj.forEach !== forEach) {
obj.forEach(iterator, context);
} else {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
iterator.call(context, obj[key], key);
iterator.call(context, obj[key], key, obj);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,26 @@ describe('angular', function() {
forEach(obj, function(value, key) { log.push(key + ':' + value); });
expect(log).toEqual(['length:2', 'foo:bar']);
});


it('should pass object being iterated over as 3rd arg to callbacks', function() {
var obj = {foo: 'bar'},
arr = [1],
func = function () {};

forEach(obj, function (value, key, collection) {
expect(obj).toBe(collection);
});

forEach(arr, function (value, index, collection) {
expect(arr).toBe(collection);
});

func.foo = 'bar';
forEach(func, function (value, key, collection) {
expect(func).toBe(collection);
});
});
});


Expand Down