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

Commit 8d7e694

Browse files
committed
fix(forEach): should ignore prototypically inherited properties
Closes #813
1 parent 5fdab52 commit 8d7e694

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/Angular.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,11 @@ function forEach(obj, iterator, context) {
117117
for (key = 0; key < obj.length; key++)
118118
iterator.call(context, obj[key], key);
119119
} else {
120-
for (key in obj)
121-
iterator.call(context, obj[key], key);
120+
for (key in obj) {
121+
if (obj.hasOwnProperty(key)) {
122+
iterator.call(context, obj[key], key);
123+
}
124+
}
122125
}
123126
}
124127
return obj;

test/AngularSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,25 @@ describe('angular', function() {
234234
});
235235
});
236236

237+
238+
describe('forEach', function() {
239+
it('should iterate over *own* object properties', function() {
240+
function MyObj() {
241+
this.bar = 'barVal';
242+
this.baz = 'bazVal';
243+
};
244+
MyObj.prototype.foo = 'fooVal';
245+
246+
var obj = new MyObj(),
247+
log = [];
248+
249+
forEach(obj, function(value, key) { log.push(key + ':' + value)});
250+
251+
expect(log).toEqual(['bar:barVal', 'baz:bazVal']);
252+
});
253+
});
254+
255+
237256
describe('sortedKeys', function() {
238257
it('should collect keys from object', function() {
239258
expect(sortedKeys({c:0, b:0, a:0})).toEqual(['a', 'b', 'c']);

0 commit comments

Comments
 (0)